[英]AttributeError: '_io.TextIOWrapper' object has no attribute 'next'?
everybody. 大家 I am currently working to merge the csv files.
我目前正在合并csv文件。 For example, you have files from filename1 to filename100.
例如,您有文件名从filename1到filename100。 I used the following code to combine 100 files and the following error occurred: I'll put the code up first.
我使用以下代码合并了100个文件,并发生了以下错误:我将代码放在第一位。 import csv
导入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()
And the following error occurred when the above file was executed: 执行上述文件时,发生以下错误:
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'
I've been working on it for a few days, and I don't know what to do. 我已经研究了几天,但我不知道该怎么办。
The file object doesn't have next
method. 文件对象没有
next
方法。 Instead use next(f)
to skip the first line 而是使用
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)
csv库中的方法“ next()”已更新为python 3中的next ()。您可以在以下链接中查看详细信息: https : //docs.python.org/3/library/csv.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.