大家。 我目前正在努力合并 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'
我已经研究了几天了,但我不知道该怎么办。
最佳答案
文件对象没有 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)
关于python - 属性错误: '_io.TextIOWrapper' object has no attribute 'next' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52134716/