【发布时间】:2015-03-11 03:36:24
【问题描述】:
我应该如何在 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:])
这会带来内存问题!
【问题讨论】:
-
你为什么要这样做?
-
因为我想去掉标题行,以后这些数据会被合并,编辑起来会比较麻烦。
-
既然可以推迟到合并文件时跳过标题行,为什么还要浪费时间?
标签: python csv python-3.x bigdata