删除大 CSV 文件的第一行?

Removing first line of Big CSV file?

我应该如何删除 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)

使用 f.__next__() 而不是 f.next()

文档在这里: https://docs.python.org/3/library/stdtypes.html#iterator.next

使用 sed 可能是最快的并且不需要临时文件,因此 python 包装器将是:

import subprocess

def delete_first_lines(filename, line_nums):
    n = '1,{}d'.format(line_nums)
    subprocess.Popen(['sed', '-i', n, filename ],
        stdout=subprocess.PIPE,
        stderr=subprocess.STDOUT
        )