繁体   English   中英

AttributeError:'_io.TextIOWrapper'对象没有属性'next'?

[英]AttributeError: '_io.TextIOWrapper' object has no attribute 'next'?

大家 我目前正在合并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)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM