3

I have a text files Text file

>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|E10D
Sequence unavailable
>E8|E2|EKB
Cricket

I wrote the following code for detecting sequence unavailable from the text file and write it in a new text file

lastline = None
with open('output.txt', 'w') as W:
    with open('input.txt', 'r') as f:
        for line in f.readlines():
            if not lastline:
                lastline = line.rstrip('\n')
                continue
            if line.rstrip('\n') == 'Sequence unavailable':
                _, _, id = lastline.split('|')
                data= 'Sequence unavailable|' + id
                W.write(data)
                W.write('\n')
            lastline = None

It work fine , it detect the sequence unavailabe from the text file and write it in a new file , but i want it to delete it from the file which it read from like

input.txt

>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|E10D
Sequence unavailable
>E8|E2|EKB
Cricket

input after code should be like this

>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|EKB
Cricket
4

3 回答 3

2

这里我没有使用file.readlines方法,因为它将文件中的所有行提取到一个列表中。因此,它不是内存有效的。

方法一:使用临时文件。

import os
with open('input.txt') as f1, open('output.txt', 'w') as f2,\
                                                  open('temp_file','w') as f3:
    lines = []       # store lines between two `>` in this list
    for line in f1:
        if line.startswith('>'):
            if lines:
                f3.writelines(lines)
                lines = [line]
            else:
                lines.append(line)
        elif line.rstrip('\n') == 'Sequence unavailable':
            f2.writelines(lines + [line])
            lines = []
        else:
            lines.append(line)
    
    f3.writelines(lines)

os.remove('input.txt')
os.rename('temp_file', 'input.txt')

演示:

$ cat input.txt
>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|E10D
Sequence unavailable
>E8|E2|EKB
Cricket

$ python so.py

$ cat input.txt
>E8|E2|E9D
Football is a good game
Its good for health
you can play it every day
>E8|E2|EKB
Cricket
$ cat output.txt
>E8|E2|E10D
Sequence unavailable

要生成临时文件,您还可以使用该tempfile模块。

方法二:文件输入模块

使用此方法不需要临时文件:

import fileinput
with open('output.txt', 'w') as f2:
    lines = []
    for line in fileinput.input('input.txt', inplace = True):
        if line.startswith('>'):
             if lines:
                 print "".join(lines),
                 lines = [line]
             else:
                 lines.append(line)
        elif line.rstrip('\n') == 'Sequence unavailable':
             f2.writelines(lines + [line])
             lines = []
        else:
             lines.append(line)

    with open('input.txt','a') as f:
        f.writelines(lines)


        
    
        
        
        
    
于 2013-07-25T13:49:41.743 回答
0

你做对了。

完成后,您只需将文件“output.txt”重命名为“input.txt”。

(不,没有简单的方法可以直接从您打开的文件中剪切一行。)

于 2013-07-25T13:33:51.790 回答
0
import os
os.system("cp output.txt input.txt")

这将使用已删除行的输出文件覆盖您的输入。 mv也可用于重命名

os.system("mv output.txt input.txt")

这将只保留一个文件,同时cp保留两个文件

您可能应该使用 os.rename()

于 2013-07-25T13:42:36.780 回答