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

임종훈*_*임종훈 2 python csv

大家。我目前正在努力合并 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()
Run Code Online (Sandbox Code Playgroud)

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

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'
Run Code Online (Sandbox Code Playgroud)

我已经研究了几天了,我不知道该怎么办。

Sun*_*tha 5

文件对象没有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)
Run Code Online (Sandbox Code Playgroud)