delete from pattern to pattern if it contains match

harirammanohar at gmail.com harirammanohar at gmail.com
Fri Apr 22 04:59:09 EDT 2016


On Thursday, April 21, 2016 at 7:03:00 PM UTC+5:30, Jussi Piitulainen wrote:
> harirammanohar at gmail.com writes:
> 
> > On Monday, April 18, 2016 at 12:38:03 PM UTC+5:30,
> > hariram... at gmail.com wrote:
> >> HI All,
> >> 
> >> can you help me out in doing below. 
> >> 
> >> file: 
> >> <start> 
> >>  guava 
> >> fruit 
> >> <end> 
> >> <start> 
> >>  mango 
> >> fruit 
> >> <end> 
> >> <start> 
> >>  orange 
> >> fruit 
> >> <end> 
> >> 
> >> need to delete from start to end if it contains mango in a file...
> >> 
> >> output should be: 
> >> 
> >> <start> 
> >>  guava 
> >> fruit 
> >> <end> 
> >> <start> 
> >>  orange 
> >> fruit 
> >> <end> 
> >> 
> >> Thank you
> >
> > any one can guide me ? why xml tree parsing is not working if i have
> > root.tag and root.attrib as mentioned in earlier post...
> 
> Assuming the real consists of lines between a start marker and end
> marker, a winning plan is to collect a group of lines, deal with it, and
> move on.
> 
> The following code implements something close to the plan. You need to
> adapt it a bit to have your own source of lines and to restore the end
> marker in the output and to account for your real use case and for
> differences in taste and judgment. - The plan is as described above, but
> there are many ways to implement it.
> 
> from io import StringIO
> 
> text = '''\
> <start> 
>   guava 
> fruit 
> <end> 
> <start>
>   mango
> fruit
> <end>
> <start> 
>   orange 
> fruit 
> <end> 
> '''
> 
> def records(source):
>     current = []
>     for line in source:
>         if line.startswith('<end>'):
>             yield current
>             current = []
>         else:
>             current.append(line)
> 
> def hasmango(record):
>     return any('mango' in it for it in record)
> 
> for record in records(StringIO(text)):
>     hasmango(record) or print(*record)

Hi,

not working....this is the output i am getting...

\
 <start>
   guava
 fruit

<start>
   orange
 fruit



More information about the Python-list mailing list