自动重命名 CSV 文件文件夹的代码出错
error with code to auto rename a folder of CSV files
该代码旨在自动重命名一堆 CSV
文件,这些文件称为
history (1) ... history (42)
这是事件列表:
https://drive.google.com/open?id=0B5bJvxM9TZkhYXZOSnRDVnhTbFk
这是我文件夹中文件的屏幕截图:
新名字是通过Beautiful soup从网页上截取的。新名称在格式上有两种可能性,其中大多数有 two
个部分,只有四个有 three
个部分。
当谈到重命名功能时,我遇到了 error
。
完整代码如下:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import os
events = open('events.txt', 'r')
os.chdir("C:\Users\Sayed\Downloads")
# name
for event in events:
sauce = urlopen(event).read()
soup = BeautifulSoup(sauce, 'lxml')
title = soup.find_all('title')
for x in title:
title = x.text
try:
firstName, lastNmame, country = (title.split('-'))
firstName = firstName.strip()
lastNmame = lastNmame.strip()
country = country.strip()
name = ('{} - {} - {}'.format(country, firstName, lastNmame))
for file in os.listdir():
firstName, ext = os.path.splitext(file)
os.rename(firstName, name)
except ValueError:
pass
try:
firstName, country = (title.split('-'))
firstName = firstName.strip()
country = country.strip()
name = ('{} - {}'.format(country, firstName))
for file in os.listdir():
firstName, ext = os.path.splitext(file)
os.rename(firstName, name)
except ValueError:
pass
这里是错误:
Traceback (most recent call last):
File "C:/Users/Sayed/PycharmProjects/Tutorial/pan.py", line 37, in <module>
os.rename(firstName, name)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'history (1)' -> 'European Monetary Union - Services Sentiment'
有效!
我复制了您的整个设置,包括所有 history (x)
文件和所有内容,并设法编写了 有效 .
的代码
这里是:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import os
events = open('events.txt', 'r')
os.chdir("C:\Users\Sayed\Downloads")
for i, event in enumerate(events):
sauce = urlopen(event).read()
soup = BeautifulSoup(sauce, 'lxml')
title = soup.find_all('title')[0].text.strip()
filename = "history ({}).csv".format(i+1)
newName = "{}.csv".format(title)
os.rename(filename, newName)
它是如何工作的?
首先,我们import
所有必要的libraries
和以前一样。
然后我们打开包含所有 links
的 events.txt
文件到我们要提取 titles
.
的页面
然后我们进入正题for-loop
。如果您以前没有看过我们在这里循环的内容可能看起来有点奇怪,从技术上讲,我们从第一个 element
的 events
创建了 tuples
的 list
tuple
的第一个是 index
,第二个 element
是值 - 所以这里是 event
。举个例子让你完全理解:
>>> l = ["a", "b", "c", "d"]
>>> for i, ele in enumerate(l):
... print(i, ele)
...
0 a
1 b
2 c
3 d
所以现在,我们有两个 variables
可以使用:event
(所以 link
到页面)和那个 [=26] 的 index
=] 在所有 events
中。请记住,由于 Python lists
是基于 0
的,因此 index
将从 0
变为 42
(而不是 1
变为43
) 所以我们以后用这个重命名files
的时候一定要记得加上1
.
接下来,我们从link
中得到raw data
,也就是这个event
和urllib
。然后,我们使用 BeautifulSoup
从 event
中提取 title
。这是通过在页面中找到 'title'
的 all 并通过使用 [0]
对其进行索引来获取第一个来完成的。我们需要 index
像这样 soup.find_all
return 一个 list
长度 1
并且我们想要第一个 element
。然后我们用这个 .text
得到一个 string
并在 string
上做一个 .strip()
来去掉回车符 return 和换行符 (/r/n
).
接下来,我们定义一个名为filename
的variable
,它将存储我们当前想要rename
的file
的名称。我们通过简单地获取 history (x).csv
位来获得 name
,这对所有 files
都是相同的,并将其格式化为当前 event
的 index
( file
),我们正在研究 - 请注意,我们 format
与 i+1
,而不是 i
,因为 files
从 history (1).csv
开始,而不是 history (0).csv
.
然后我们定义一个名为 newName
的变量,它只是我们想要 rename
的 filename
。这个的名字只是我们之前提取的name
(title
),然后在最后提取'.csv'
,因为我们需要保留file
extensions
一样。
最后,我们为此 file
调用 rename
。这简直就是filename
到newName
!
这对我来说绝对有用,所以我看不出有什么理由不适合你...另外,我希望你能理解代码所做的一切,如果我错过了,请在评论中提问某事...
该代码旨在自动重命名一堆 CSV
文件,这些文件称为
history (1) ... history (42)
这是事件列表: https://drive.google.com/open?id=0B5bJvxM9TZkhYXZOSnRDVnhTbFk
这是我文件夹中文件的屏幕截图:
新名字是通过Beautiful soup从网页上截取的。新名称在格式上有两种可能性,其中大多数有 two
个部分,只有四个有 three
个部分。
当谈到重命名功能时,我遇到了 error
。
完整代码如下:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import os
events = open('events.txt', 'r')
os.chdir("C:\Users\Sayed\Downloads")
# name
for event in events:
sauce = urlopen(event).read()
soup = BeautifulSoup(sauce, 'lxml')
title = soup.find_all('title')
for x in title:
title = x.text
try:
firstName, lastNmame, country = (title.split('-'))
firstName = firstName.strip()
lastNmame = lastNmame.strip()
country = country.strip()
name = ('{} - {} - {}'.format(country, firstName, lastNmame))
for file in os.listdir():
firstName, ext = os.path.splitext(file)
os.rename(firstName, name)
except ValueError:
pass
try:
firstName, country = (title.split('-'))
firstName = firstName.strip()
country = country.strip()
name = ('{} - {}'.format(country, firstName))
for file in os.listdir():
firstName, ext = os.path.splitext(file)
os.rename(firstName, name)
except ValueError:
pass
这里是错误:
Traceback (most recent call last):
File "C:/Users/Sayed/PycharmProjects/Tutorial/pan.py", line 37, in <module>
os.rename(firstName, name)
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'history (1)' -> 'European Monetary Union - Services Sentiment'
有效!
我复制了您的整个设置,包括所有 history (x)
文件和所有内容,并设法编写了 有效 .
这里是:
from urllib.request import urlopen
from bs4 import BeautifulSoup
import os
events = open('events.txt', 'r')
os.chdir("C:\Users\Sayed\Downloads")
for i, event in enumerate(events):
sauce = urlopen(event).read()
soup = BeautifulSoup(sauce, 'lxml')
title = soup.find_all('title')[0].text.strip()
filename = "history ({}).csv".format(i+1)
newName = "{}.csv".format(title)
os.rename(filename, newName)
它是如何工作的?
首先,我们import
所有必要的libraries
和以前一样。
然后我们打开包含所有 links
的 events.txt
文件到我们要提取 titles
.
然后我们进入正题for-loop
。如果您以前没有看过我们在这里循环的内容可能看起来有点奇怪,从技术上讲,我们从第一个 element
的 events
创建了 tuples
的 list
tuple
的第一个是 index
,第二个 element
是值 - 所以这里是 event
。举个例子让你完全理解:
>>> l = ["a", "b", "c", "d"]
>>> for i, ele in enumerate(l):
... print(i, ele)
...
0 a
1 b
2 c
3 d
所以现在,我们有两个 variables
可以使用:event
(所以 link
到页面)和那个 [=26] 的 index
=] 在所有 events
中。请记住,由于 Python lists
是基于 0
的,因此 index
将从 0
变为 42
(而不是 1
变为43
) 所以我们以后用这个重命名files
的时候一定要记得加上1
.
接下来,我们从link
中得到raw data
,也就是这个event
和urllib
。然后,我们使用 BeautifulSoup
从 event
中提取 title
。这是通过在页面中找到 'title'
的 all 并通过使用 [0]
对其进行索引来获取第一个来完成的。我们需要 index
像这样 soup.find_all
return 一个 list
长度 1
并且我们想要第一个 element
。然后我们用这个 .text
得到一个 string
并在 string
上做一个 .strip()
来去掉回车符 return 和换行符 (/r/n
).
接下来,我们定义一个名为filename
的variable
,它将存储我们当前想要rename
的file
的名称。我们通过简单地获取 history (x).csv
位来获得 name
,这对所有 files
都是相同的,并将其格式化为当前 event
的 index
( file
),我们正在研究 - 请注意,我们 format
与 i+1
,而不是 i
,因为 files
从 history (1).csv
开始,而不是 history (0).csv
.
然后我们定义一个名为 newName
的变量,它只是我们想要 rename
的 filename
。这个的名字只是我们之前提取的name
(title
),然后在最后提取'.csv'
,因为我们需要保留file
extensions
一样。
最后,我们为此 file
调用 rename
。这简直就是filename
到newName
!
这对我来说绝对有用,所以我看不出有什么理由不适合你...另外,我希望你能理解代码所做的一切,如果我错过了,请在评论中提问某事...