Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #107485

Re: delete from pattern to pattern if it contains match

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: delete from pattern to pattern if it contains match
Date Fri, 22 Apr 2016 11:24:06 +0200
Organization None
Lines 133
Message-ID <mailman.11.1461317067.2861.python-list@python.org> (permalink)
References <20c0b0fe-136b-4b01-b004-c55c6d47b299@googlegroups.com> <91432d7b-7233-4504-a725-22bc81637ea3@googlegroups.com> <lf5fuufqe81.fsf@ling.helsinki.fi> <991c5867-27d1-4e75-aa52-a7d47e626b74@googlegroups.com> <nfcqjs$guu$1@ger.gmane.org>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Trace news.uni-berlin.de 6MeqNeJP3e/0umNY3DbO6QvsNWg+7m2fSGQIHCXgdrQA==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'lines,': 0.05; '21,': 0.07; 'implements': 0.07; 'inserts': 0.07; 'trailing': 0.07; "'''": 0.09; 'orange': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'specifying': 0.09; 'stringio': 0.09; 'output': 0.13; 'thursday,': 0.13; 'def': 0.13; '"..."': 0.16; '2016': 0.16; 'delimiters': 0.16; 'first)': 0.16; 'fruit': 0.16; 'marker': 0.16; 'plan.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.16; 'script.': 0.18; 'tree': 0.18; 'all,': 0.20; 'changes': 0.20; 'fix': 0.21; 'assuming': 0.22; 'file:': 0.22; 'parsing': 0.22; 'bit': 0.23; 'forgot': 0.23; 'import': 0.24; 'xml': 0.24; 'header:User- Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'earlier': 0.27; 'entries': 0.27; 'yield': 0.27; 'looks': 0.29; 'record': 0.29; 'be:': 0.29; 'spaces': 0.29; 'whitespace': 0.29; 'code': 0.30; 'implement': 0.32; 'source': 0.33; 'except': 0.34; 'add': 0.34; 'text': 0.35; 'something': 0.35; 'but': 0.36; 'should': 0.36; 'there': 0.36; 'lines': 0.36; 'beginning': 0.36; 'monday,': 0.36; 'winning': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'doing': 0.38; 'delete': 0.38; 'thank': 0.38; 'hi,': 0.38; 'end': 0.39; 'means': 0.39; 'why': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'still': 0.40; 'received:de': 0.40; 'your': 0.60; 'close': 0.61; 'avoid': 0.61; 'email addr:gmail.com': 0.62; 'real': 0.62; 'above,': 0.63; 'between': 0.65; 'differences': 0.66; 'below.': 0.66; 'account': 0.66; 'restore': 0.70; 'source:': 0.84; 'utc+5:30,': 0.84
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host p57bd9335.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <nfcqjs$guu$1@ger.gmane.org>
X-Mailman-Original-References <20c0b0fe-136b-4b01-b004-c55c6d47b299@googlegroups.com> <91432d7b-7233-4504-a725-22bc81637ea3@googlegroups.com> <lf5fuufqe81.fsf@ling.helsinki.fi> <991c5867-27d1-4e75-aa52-a7d47e626b74@googlegroups.com>
Xref csiph.com comp.lang.python:107485

Show key headers only | View raw


harirammanohar@gmail.com wrote:

> On Thursday, April 21, 2016 at 7:03:00 PM UTC+5:30, Jussi Piitulainen
> wrote:
>> harirammanohar@gmail.com writes:
>> 
>> > On Monday, April 18, 2016 at 12:38:03 PM UTC+5:30,
>> > hariram...@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...
> 
> \

This means that the line

>> text = '''\

has trailing whitespace in your copy of the script.

>  <start>
>    guava
>  fruit
> 
> <start>
>    orange
>  fruit

Jussi forgot to add the "<end>..." line to the group. To fix this change the 
generator to

def records(source):
    current = []
    for line in source:
        current.append(line)
        if line.startswith('<end>'):
            yield current
            current = []


>>     hasmango(record) or print(*record)

The

print(*record)

inserts spaces between record entries (i. e. at the beginning of all lines 
except the first) and adds a trailing newline. You can avoid this by 
specifying the delimiters explicitly:

if not hasmango(record):
    print(*record, sep="", end="")

Even with these changes code still looks somewhat brittle...

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-18 00:07 -0700
  RE: delete from pattern to pattern if it contains match Joaquin Alzola <Joaquin.Alzola@lebara.com> - 2016-04-18 07:49 +0000
    Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-18 01:52 -0700
    Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-18 21:01 -0700
  Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-21 03:17 -0700
    Re: delete from pattern to pattern if it contains match Peter Otten <__peter__@web.de> - 2016-04-21 13:24 +0200
      Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-22 02:00 -0700
        Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-22 02:14 -0700
          Re: delete from pattern to pattern if it contains match Peter Otten <__peter__@web.de> - 2016-04-22 11:50 +0200
            Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-24 23:24 -0700
    Re: delete from pattern to pattern if it contains match Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-21 16:32 +0300
      Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-22 01:59 -0700
        Re: delete from pattern to pattern if it contains match Peter Otten <__peter__@web.de> - 2016-04-22 11:24 +0200
          Re: delete from pattern to pattern if it contains match Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-22 14:10 +0300
            Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-24 23:29 -0700
              Re: delete from pattern to pattern if it contains match Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-25 10:17 +0300
                Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-25 02:49 -0700
                Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-25 02:53 -0700
                Re: delete from pattern to pattern if it contains match Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-25 13:37 +0300
                Re: delete from pattern to pattern if it contains match Peter Otten <__peter__@web.de> - 2016-04-25 12:13 +0200
                Re: delete from pattern to pattern if it contains match Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-25 13:39 +0300
                Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-25 04:02 -0700
                Re: delete from pattern to pattern if it contains match Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-25 14:28 +0300
                Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-25 04:40 -0700
                Re: delete from pattern to pattern if it contains match Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-25 15:00 +0300
                Re: delete from pattern to pattern if it contains match Peter Otten <__peter__@web.de> - 2016-04-25 14:33 +0200
                Re: delete from pattern to pattern if it contains match harirammanohar@gmail.com - 2016-04-26 03:31 -0700
                Re: delete from pattern to pattern if it contains match Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-25 13:24 +0300
                RE: delete from pattern to pattern if it contains match Joaquin Alzola <Joaquin.Alzola@lebara.com> - 2016-04-25 10:19 +0000

csiph-web