简体   繁体   English

读取和删除 python 中 N 行的最快方法

[英]Fastest way to read and delete N lines in python

Fastest way to read and delete N lines in python.读取和删除 python 中N行的最快方法。

First I read the file something like this: (I think this is the best way to read large files: Source )首先,我读取文件是这样的:(我认为这是读取大文件的最佳方式: 来源

N = 50
with open("ahref.txt", "r+") as f:
    link_list = [(next(f)).removesuffix("\n") for x in range(N)]

after that I run my code:之后我运行我的代码:

# My code here

After that I want to delete the first N line (I read it: Source ).之后我想删除第一个N行(我读过它: Source )。

# Source: https://stackoverflow.com/questions/4710067/how-to-delete-a-specific-line-in-a-file/28057753#28057753
with open("target.txt", "r+") as f:
    d = f.readlines()
    f.seek(0)
    for i in d:
        if i != "line you want to remove...":
            f.write(i)
    f.truncate()

This code doesn't work for me.此代码对我不起作用。 Because I read only N numbers of lines.因为我只读了N行。

I can remove lines:我可以删除行:

with open("xml\\ahref.txt", "r+") as f:
    N = 5
    all_lines = f.readlines()
    f.seek(0)
    f.truncate()
    f.writelines(all_lines[N:])

But there is a problem with that:但这有一个问题:

  1. I have to read all the lines and after that I have to write all the lines.我必须阅读所有的台词,然后我必须写下所有的台词。 which is not a fast way (There are many ways, but it needs to read all line)这不是一个快速的方法(有很多方法,但它需要读取所有行)

What is the fastest way in terms of performance?就性能而言最快的方法是什么? because the file is huge.因为文件很大。

fastest way is not to read the entire file in memory and use a temporary output file that you can then move over the original file if required最快的方法不是读取 memory 中的整个文件,而是使用一个临时的 output 文件,如果需要,您可以将其移至原始文件

try:尝试:

N = 50
mode = "r+"
if not os.path.isfile('output'): mode = "w+" 
with open('input', 'r') as fin, open('output', mode) as fout:
    for index, line in enumerate(fout): N += 1
    for index, line in enumerate(fin):
        if index > N: fout.write(line)
        # i haven't tested this you may need index > N or index >= N

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM