“蚕食"最有效的方式文本文档中的第一行文本,然后将其重新保存在 python 中 [英] Most efficient way to "nibble" the first line of text from a text document then resave it in python

查看:23
本文介绍了“蚕食"最有效的方式文本文档中的第一行文本,然后将其重新保存在 python 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本文档,我想每隔 30 秒左右重复删除第一行文本.

I have a text document that I would like to repeatedly remove the first line of text from every 30 seconds or so.

我已经编写(或更准确地说是复制了)python 可重置计时器对象的代码,该对象允许在不要求重置或取消的情况下以非阻塞方式每 30 秒调用一次函数.

I have already written (or more accurately copied) the code for the python resettable timer object that allows a function to be called every 30 seconds in a non blocking way if not asked to reset or cancel.

python 中的可重置计时器重复直到取消

(如果有人可以检查我实现重复的方式,那没关系,因为我的 python 在运行时有时会崩溃,将不胜感激:))

(If someone could check the way I implemented the repeat in that is ok, because my python sometimes crashes while running that, would be appreciated :))

我现在想编写我的函数来加载一个文本文件,并可能复制除第一行之外的所有内容,然后将其重写为同一个文本文件.我可以这样做,我认为这样...但它是最有效的吗?

I now want to write my function to load a text file and perhaps copy all but the first line and then rewrite it to the same text file. I can do this, this way I think... but is it the most efficient ?

def removeLine():

    with open(path, 'rU') as file:
        lines = deque(file)
        try:
            print lines.popleft()
        except IndexError:
            print "Nothing to pop?"
    with open(path, 'w') as file:
        file.writelines(lines)  

这行得通,但这是最好的方法吗?

This works, but is it the best way to do it ?

推荐答案

我会使用 fileinput 模块 with inplace=True:

import fileinput

def removeLine():
    inputfile = fileinput.input(path, inplace=True, mode='rU')
    next(inputfile, None)  # skip a line *if present*
    for line in inputfile:
        print line,  # write out again, but without an extra newline
    inputfile.close()

inplace=True 导致 sys.stdout 被重定向到打开的文件,所以我们可以简单地打印"这些行.

inplace=True causes sys.stdout to be redirected to the open file, so we can simply 'print' the lines.

next() 调用用于跳过第一行;给它一个默认的 None 会抑制一个空文件的 StopIteration 异常.

The next() call is used to skip the first line; giving it a default None suppresses the StopIteration exception for an empty file.

这使得重写文件更加高效,因为您只需要将fileinput readlines 缓冲区保留在内存中.

This makes rewriting a large file more efficient as you only need to keep the fileinput readlines buffer in memory.

我认为根本不需要 deque,即使对于您的解决方案;只需在那里使用 next() ,然后使用 list() 来捕获剩余的行:

I don't think a deque is needed at all, even for your solution; just use next() there too, then use list() to catch the remaining lines:

def removeLine():
    with open(path, 'rU') as file:
        next(file, None)  # skip a line *if present*
        lines = list(file)
    with open(path, 'w') as file:
        file.writelines(lines)  

但这需要你读取内存中的所有文件;不要对大文件这样做.

but this requires you to read all of the file in memory; don't do that with large files.

这篇关于“蚕食"最有效的方式文本文档中的第一行文本,然后将其重新保存在 python 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆