在python中删除文本文件的某些行 [英] Deleting certain line of text file in python

查看:67
本文介绍了在python中删除文本文件的某些行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下文本文件:

This is my text file
NUM,123
FRUIT
DRINK
FOOD,BACON
CAR
NUM,456
FRUIT
DRINK
FOOD,BURGER
CAR
NUM,789
FRUIT
DRINK
FOOD,SAUSAGE
CAR
NUM,012
FRUIT
DRINK
FOOD,MEATBALL
CAR

并且我有以下列表称为需要":

And I have the following list called 'wanted':

['123', '789']

我想做的是,如果NUM之后的数字不在"wanted"列表中,则该行及其下的4行将被删除.因此输出文件将如下所示:

What I'm trying to do is if the numbers after NUM is not in the list called 'wanted', then that line along with 4 lines below it gets deleted. So the output file will looks like:

This is my text file
NUM,123
FRUIT
DRINK
FOOD,BACON
CAR
NUM,789
FRUIT
DRINK
FOOD,SAUSAGE
CAR

到目前为止,我的代码是:

My code so far is:

infile = open("inputfile.txt",'r')
data = infile.readlines()

for beginning_line, ube_line in enumerate(data):
    UNIT = data[beginning_line].split(',')[1]
    if UNIT not in wanted:
        del data_list[beginning_line:beginning_line+4]

推荐答案

在遍历列表时,请勿修改列表.

You shouldn't modify a list while you are looping over it.

您可以尝试在需要时在文件对象上推进迭代器:

What you could try is to just advance the iterator on the file object when needed:

wanted = set(['123', '789'])

with open("inputfile.txt",'r') as infile, open("outfile.txt",'w') as outfile: 
    for line in infile:
        if line.startswith('NUM,'):
            UNIT = line.strip().split(',')[1] 
            if UNIT not in wanted:
                for _ in xrange(4):
                    infile.next()
                continue

        outfile.write(line)

并使用一组.不断检查成员身份的速度更快.

And use a set. It is faster for constantly checking the membership.

这种方法不会使您立即读入整个文件以列表形式对其进行处理.它逐行,从文件读取,前进并写入新文件.如果需要,可以用要附加的列表替换输出文件.

This approach doesn't make you read in the entire file at once to process it in a list form. It goes line by line, reading from the file, advancing, and writing to the new file. If you want, you can replace the outfile with a list that you are appending to.

这篇关于在python中删除文本文件的某些行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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