Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.049 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'subject:text': 0.05; 'lines,': 0.07; 'section,': 0.09; 'subject:into': 0.09; 'suggestions.': 0.09; 'python': 0.11; 'language,': 0.12; 'sections': 0.14; 'begin.': 0.16; 'keys.': 0.16; 'all.': 0.16; 'all,': 0.19; 'split': 0.19; 'starts': 0.20; 'thanks.': 0.20; '(the': 0.22; 'programming': 0.22; 'module,': 0.24; 'section.': 0.24; 'text.': 0.24; 'tried': 0.27; 'rest': 0.29; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; "skip:' 10": 0.31; 'keys': 0.31; 'perl': 0.31; 'regular': 0.32; 'text': 0.33; 'beginning': 0.33; 'maybe': 0.34; 'problem': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'method': 0.36; 'should': 0.36; 'so,': 0.37; 'list': 0.37; 'clear': 0.37; 'thank': 0.38; 'to:addr:python-list': 0.38; 'explain': 0.39; 'does': 0.39; 'expensive': 0.39; 'quote': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'how': 0.40; 'even': 0.60; 'easy': 0.60; 'expression': 0.60; 'solve': 0.60; 'hope': 0.61; 'length': 0.61; 'first': 0.61; 'more': 0.64; 'different': 0.65; 'situation': 0.65; 'here': 0.66; 'line,': 0.68; 'special': 0.74; 'subject:this': 0.83; 'const': 0.84; 'items,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=Y5lLpIPUQwXoiGX6onFblh3hEMOnj/qa5LHJffnucC0=; b=PZ0MOS+LWA9BKM98sA35arS7t7pvJZESg5B3NNY+LFFZKxGFWAFc2pZKQxu1qjt/LN MdXlBdueey88ZpLTD/X/yV6ai+CZcsgCRMUOOKKG5ktJQyYu9ye08R+mocRZtMVEcyYL APgfe0L3GH4SRWyVdRadHMtUTfb8dgGaYtClNs6U6FwxykZL5kK27r35g/H7gA9TIbfy /dodJ2tSM1sLRoGXadqVUfk+NlxZDRZeACaSmJNCOg5HsPckKKeqGWkFaMga10kPJTEd 4oaYUS1/jSJB2zTROM1ttEFkzKDuxdiJjveeHX4wwEaK6OGQLhqNer0JGpwXcOnLXBJc Goig== MIME-Version: 1.0 X-Received: by 10.50.114.41 with SMTP id jd9mr12096249igb.33.1398527594499; Sat, 26 Apr 2014 08:53:14 -0700 (PDT) Date: Sat, 26 Apr 2014 23:53:14 +0800 Subject: Re: how to split this kind of text into sections From: oyster To: python-list@python.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 82 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398527597 news.xs4all.nl 2960 [2001:888:2000:d::a6]:53865 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70634 First of all, thank you all for your answers. I received python mail-list in a daily digest, so it is not easy for me to quote your mail separately. I will try to explain my situation to my best, but English is not my native language, I don't know whether I can make it clear at last. Every SECTION starts with 2 special lines; these 2 lines is special because they have some same characters (the length is not const for different section) at the beginning; these same characters is called the KEY for this section. For every 2 neighbor sections, they have different KEYs. After these 2 special lines, some paragraph is followed. Paragraph does not have any KEYs. So, a section =3D 2 special lines with KEYs at the beginning + some paragraph without KEYs However there maybe some paragraph before the first section, which I do not need and want to drop it I need a method to split the whole text into SECTIONs and to know all the K= EYs I have tried to solve this problem via re module, but failed. Maybe I can make you understand me clearly by showing the regular expression object reobj =3D re.compile(r"(?P[^\r\n]*?)[^\r\n]*?\r\n(?P=3Dbookname)[= ^\r\n]*?\r\n.*?", re.DOTALL) which can get the first 2 lines of a section, but fail to get the rest of a section which does not have any KEYs at the begin. The hard part for me is to express "paragraph does not have KEYs". Even I can get the first 2 line, I think regular expression is expensive for my text. That is all. I hope get some more suggestions. Thanks. [demo text starts] a line we do not need I am section axax I am section bbb (and here goes many other text)... let's continue to let's continue, yeah .....(and here goes many other text)... I am using python I am using perl .....(and here goes many other text)... Programming is hard Programming is easy How do you thing? I do=E2=80=99t know [demo text ends] the above text should be splited to a LIST with 4 items, and I also need to know the KEY for LIST is ['I am section ', 'let's continue', 'I am using ', ' Programming is ']: lst=3D[ '''a line we do not need I am section axax I am section bbb (and here goes many other text)... ''', '''let's continue to let's continue, yeah .....(and here goes many other text)... ''', '''I am using python I am using perl .....(and here goes many other text)... ''', '''Programming is hard Programming is easy How do you thing? I do=E2=80=99t know''' ]