我应该如何在 python 中删除一个大的 CSV 文件的第一行? 我在这里查看了以前的解决方案,一个是:
with open("test.csv",'r') as f:
with open("updated_test.csv",'w') as f1:
f.next() # skip header line
for line in f:
f1.write(line)
这给了我这个错误:
f.next() # skip header line
AttributeError: '_io.TextIOWrapper' object has no attribute 'next'
另一个解决方案是:
with open('file.txt', 'r') as fin:
data = fin.read().splitlines(True)
with open('file.txt', 'w') as fout:
fout.writelines(data[1:])
这会带来内存问题!
最佳答案
将 f.next()
替换为 next(f)
with open("test.csv",'r') as f, open("updated_test.csv",'w') as f1:
next(f) # skip header line
for line in f:
f1.write(line)
关于python - 删除大 CSV 文件的第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27917760/