自动重命名 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和以前一样。

然后我们打开包含所有 linksevents.txt 文件到我们要提取 titles.

的页面

然后我们进入正题for-loop。如果您以前没有看过我们在这里循环的内容可能看起来有点奇怪,从技术上讲,我们从第一个 elementevents 创建了 tupleslist 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,也就是这个eventurllib。然后,我们使用 BeautifulSoupevent 中提取 title。这是通过在页面中找到 'title'all 并通过使用 [0] 对其进行索引来获取第一个来完成的。我们需要 index 像这样 soup.find_all return 一个 list 长度 1 并且我们想要第一个 element。然后我们用这个 .text 得到一个 string 并在 string 上做一个 .strip() 来去掉回车符 return 和换行符 (/r/n).

接下来,我们定义一个名为filenamevariable,它将存储我们当前想要renamefile 的名称。我们通过简单地获取 history (x).csv 位来获得 name,这对所有 files 都是相同的,并将其格式化为当前 eventindexfile),我们正在研究 - 请注意,我们 formati+1,而不是 i,因为 fileshistory (1).csv 开始,而不是 history (0).csv.

然后我们定义一个名为 newName 的变量,它只是我们想要 renamefilename。这个的名字只是我们之前提取的nametitle),然后在最后提取'.csv',因为我们需要保留file extensions一样。

最后,我们为此 file 调用 rename。这简直就是filenamenewName!


这对我来说绝对有用,所以我看不出有什么理由不适合你...另外,我希望你能理解代码所做的一切,如果我错过了,请在评论中提问某事...