简体   繁体   English

解析文件,将每行添加到末尾,然后从顶部删除该行

[英]parse a file, appending each line at the end and removing the line from the top

I am trying to move each line down at the bottom of the file; 我正在尝试将文件底部的每一行向下移动; this is how the file look like: 文件是这样的:

daodaos  12391039
idiejda  94093420
jfijdsf  10903213
....
#completed

So at the end of the parsing, I am planning to get all the entry that are on the top, under the actual string that says # completed. 因此,在解析结束时,我计划在显示#的实际字符串下获取顶部的所有条目。

The problem is that I am not sure how can I do this in one pass; 问题是我不确定如何一次完成此操作。 I know that I can read the whole file, every single line, close the file and then re-open the file in write mode; 我知道我可以读取整个文件,每一行,关闭文件,然后以写入模式重新打开文件; searching for that line, removing it from the file and adding it to the end; 搜索该行,将其从文件中删除并将其添加到末尾; but it feels incredibly inefficient. 但感觉效率极低。

Is there a way in one pass, to process the current line; 有没有一种方法可以处理当前行; then in the same for loop, delete the line and append it at the end of the file? 然后在同一for循环中,删除该行并将其附加在文件末尾?

file = open('myfile.txt', 'a')
for items in file:
    #process items line
    #append items line to the end of the file
    #remove items line from the file

Not really. 并不是的。

Your main problem here is that you're iterating on the file at the same time you want to change it. 您的主要问题是要在更改文件的同时对文件进行迭代。 This will Do Bad Things (tm) to your processing, unless you plan to micro-manage the file position pointer. 除非您打算对文件位置指针进行微管理,否则这将对您的处理产生不良影响(tm)。

You do have that power: the seek method lets you move to a given file location, expressed in bytes. 您确实具有这种能力: seek方法可让您移动到给定的文件位置,以字节为单位。 seek(0) moves to the start of the file; seek(0)移到文件的开头; seek(-1) to the end. 搜寻(-1)到底。 The problem you face is that your for loop trusts that this pointer indicates the next line to read. 您面临的问题是您的for循环信任此指针指示要读取的下一行。

One distinct problem is that you can't just remove a line from the middle of the file; 一个明显的问题是您不能只从文件中间删除一行; something exists in those bytes. 这些字节中存在某些内容 Think of it as lines of text on a page, written in pencil. 可以将它视为用铅笔写在页面上的文本行。 You can erase line 4, but this does not cause lines 5-end to magically float up half a centimeter; 您可以擦除第4行,但这不会导致第5行的末端神奇地漂浮半厘米。 they're still in the same physical location. 他们仍然在同一物理位置。

How to Do It ... sort of 怎么做...有点

Read all of the lines into a list. 将所有行读入列表。 You can easily change a list the way you want. 您可以轻松地以所需方式更改列表。 When you hit the end, then write the list back to the file -- or use your magic seek and append powers to alter only a little of it. 结束时,将列表写回到文件中-或使用魔术搜索附加功能仅更改其中一部分。

我建议您以简单的方式进行操作:读取所有文件并将其存储在变量中,将完成的文件移至另一个变量,然后重写文件。

suggest to keep it simple read and writeback 建议保持简单的读写

with open('myfile.txt') as f:
    lines = f.readlines()
with open('myfile.txt', 'w') as f:
    newlines = []
    for line in lines:
        # do you stuff, check if completed, rearrange the list
        if line.startswith('#completed'):
            idx=i
            newlines = lines[idx:] + lines[:idx]
            break
    f.write(''.join(newlines)) # write back new lines

below is another version i could think of if insist wanna modify while reading 下面是我可能想到的另一个版本,如果在阅读时坚持要修改

with open('myfile.txt', 'r+') as f:
    newlines = ''
    line = True
    while line:
        line = f.readline()
        if line.startswith('#completed'):
            # line += f.read() # uncomment this line if you interest on line after #completed
            f.truncate()
            f.seek(0)
            f.write(line + newlines)
            break
        else:
          newlines += line

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

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