gpt4 book ai didi

python - 删除大 CSV 文件的第一行?

转载 作者:太空狗 更新时间:2023-10-29 22:18:56 27 4
gpt4 key购买 nike

我应该如何在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com