2017-04-26 77 views
2

我有一个文件,其中包含以下行。 (请注意新行)从给定关键字的文件中删除一定数量的行python

blah blah blah 

ID:name1:1bj409ju9 
how are you 

Im good 100 
blah blah 

ID:name2:987krjtu 
not so good 

too bad 900 
blah blah 

some words blah blah 

当您注意到以“ID”开头的行有一个模式。我尝试搜索ID:name [x]并删除5行(包括空格)。例如,我想从文件中删除下面的一组行。

ID:name1:10.1.1.10 
how are you 

I'm good 100 
blah blah 

我试着下面的代码,但它仅删除匹配“somename1”

#!/usr/bin/python 
import fileinput 

filename = r"file.txt" 
counter = -1 
for linenum,line in enumerate(fileinput.FileInput(filename, inplace=1)): 
    if "name1" in line: 
     counter = linenum + 6 
     if linenum == counter: 
      line.strip() 
    else: 
     print line, 

请注意,我想“等等等等”和“之间摆脱了新的空行的行ID:somename2:987krjtu”。

+0

能不能是可变数量的行吗?从技术上讲,你不想删除所有行,直到下一个“ID:”行?在你的代码中,你设置了一个计数器,但是除非行中包含“name1”,否则不要使用它。如果逻辑只是在行中检查该字符串并在不存在时进行打印,则为高级别。您需要执行其他逻辑来使用您的计数器值 – Dan

+2

我不确定为什么您期望'linenum == counter',因为它在计数6大于行 –

+0

@ cricket_007后立即感谢。如前所述,我是一个初学者。请用适当的评论改正代码,以便我更好地理解。说实话,我非常盲目地提出了这个代码。 – bindo

回答

2

你可以尝试:

def delete_lines(name, finput): 
    for line in finput: 
     if line.startswith('ID:') and line.contains(name): 
      # iterate finput five times 
      for i in range(5): 
       next(finput) 
     else: 
      # print the other lines 
      print(line) 
      # if you want to have the remaining lines in a variable you could also yield them 
      yield(line) 

然后调用函数:

lines = list(delete_lines('name1', fileinput.FileInput(filename, inplace=1))) 

线将包含未被删除的所有行。

注意,同样的方法也应该打开的文件描述符工作:

with open(filename, 'rt') as finput: 
    delete_lines('name1', finput) 

或与内存线列表(如果你不关心加载在内存中的完整的文件):

with open(filename, 'rt') as finput: 
    lines = finput.readlines() 
delete_lines('name1', finput) 
0

如果你的文件可以加载到内存中,如果你想two patterns之间删除使用正则表达式

import re 
with open(fn) as f: 
    result=re.sub(r'^ID:name1[\s\S]*(?=^ID:name2.*)','',f.read(),0,re.M) 
    print result 

模式的说明:

^ID:name1[\s\S]*(?=^ID:name2.*) 
^         Start of line 
    ^        First pattern 
     ^      A space and not a space - 
             a way of saying anything including new lines 
       ^.     greedy -- all of them 
        ^   stop before the end pattern 

如果你想跟随的匹配线(与两个锚)n号线可以使用this regex

with open(fn) as f: 
    result=re.sub(r'^ID:name1.*\s(^.*$\s){1,5}','',f.read(),0,re.M) 
    print result 

移出这种模式的:

^ID:name1.*\s(^.*$\s){1,5} 
    ^ ^     start pattern line 

       ^    1 to five lines following 
相关问题