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

标签 python csv python-3.x bigdata

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

相关文章:

database - influxdb 数据/表可以下载为 csv 文件吗?

php - 在 PHP 中对数字数据进行表查找

python - 如何为 python 日志记录设置 HTTPHandler

python - 在Python中比较整数中的数字

python - CSV 文件 : Change the position of column to row and reorganize dataset

python - 时区不正确

python - python的idle里面有水平滚动条吗?

python - 我不明白这个函数是如何工作的,以及它是如何得出 60 的结果的

Python 包导入冲突(Theano 和 openturns)

Python:消除数据框中重复的小数