所有人。我目前正在努力合并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'
我已经做了几天了,我不知道该怎么做。
发布于 2018-09-01 23:20:47
文件对象没有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-01 23:24:35
csv库中的方法"next()“被更新为python3中的next()。您可以在这个链接中看到详细信息:https://docs.python.org/3/library/csv.html。
https://stackoverflow.com/questions/52134716
复制