python - 属性错误: '_io.TextIOWrapper' object has no attribute 'next' ?

标签 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()

执行上述文件时出现以下错误:

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/

相关文章:

python - 过滤查询集以仅返回每个用户的最佳结果

c# - 忽略没有数据的 CSV 行

string - python : Convert string (in scientific notation) to float

数据库中的 PHP 回显表并将它们下载为 CSV 文件

python - 从创建它的函数中提取字典(python)

python - 基于路由的应用程序的架构

Python Week Numbers,其中所有周都有 7 天,无论年份是否翻转

python - Keras 值错误 : Dimensions must be equal issue

c# - 将动态 csv 转换为包含 List<string> C# 的 json

java - 将数据添加/附加到现有 CSV 文件的特定行/单元格