1

大家。我目前正在合并 csv 文件。例如,您有从 filename1 到 filename100 的文件。我用下面的代码合并了100个文件,出现如下错误:我先把代码放上去。导入 csv

fout=open("aossut.csv","a")
# first file:
for line in open("filename1.csv"):
    fout.write(line)
# now the rest:    
for num in range(2,101):
    f = open("filename"+str(num)+".csv")
    f.next() # skip the header
    for line in f:
         fout.write(line)
    f.close() # not really needed
fout.close()

并且在执行上述文件时出现以下错误:

File "C:/Users/Jangsu/AppData/Local/Programs/Python/Python36-32/tal.py", line 10, in 
<module>
    f.next() # skip the header
AttributeError: '_io.TextIOWrapper' object has no attribute 'next'

这几天我一直在努力,我不知道该怎么办。

4

2 回答 2

4

文件对象没有next方法。而是使用next(f)跳过第一行

for num in range(2,101):
    with open("filename"+str(num)+".csv") as f:
        next(f)
        for line in f:
            fout.write(line)
于 2018-09-02T07:20:47.837 回答
0

csv 库中的方法“next()”在 python 3 中更新为next ()。您可以在此链接中查看详细信息:https ://docs.python.org/3/library/csv.html

于 2018-09-02T07:24:35.843 回答