Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'subject:text': 0.05; 'section,': 0.09; 'subject:into': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'language,': 0.12; 'stored': 0.12; 'sections': 0.14; '-tkc': 0.16; 'collections': 0.16; 'defaultdict': 0.16; 'delimit': 0.16; 'follow-up': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'itertools': 0.16; 'keys.': 0.16; 'lambda': 0.16; 'len(line)': 0.16; 'line)': 0.16; 'oyster': 0.16; 'preamble': 0.16; ':-)': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'module': 0.19; 'split': 0.19; 'starts': 0.20; 'written': 0.21; '(the': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'section.': 0.24; 'earlier': 0.24; 'initial': 0.24; '(or': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; "i've": 0.25; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'character': 0.29; 'characters': 0.30; 'code': 0.31; 'lines': 0.31; 'keys': 0.31; 'stuff': 0.32; 'text': 0.33; 'beginning': 0.33; 'sense': 0.34; 'problem': 0.35; 'but': 0.35; 'method': 0.36; 'charset:us-ascii': 0.36; 'should': 0.36; 'clear': 0.37; 'minimum': 0.38; 'skip:o 20': 0.38; 'that,': 0.38; 'explain': 0.39; 'delete': 0.39; 'either': 0.39; 'called': 0.40; 'solve': 0.60; 'length': 0.61; 'such': 0.63; 'more': 0.64; 'different': 0.65; '(that': 0.65; 'situation': 0.65; 'to:addr:gmail.com': 0.65; 'reply': 0.66; 'between': 0.67; 'below.': 0.71; 'special': 0.74; 'subject:this': 0.83; 'const': 0.84; 'received:50.22': 0.84; 'words)': 0.84 Date: Sat, 26 Apr 2014 11:59:56 -0500 From: Tim Chase To: oyster Subject: Re: how to split this kind of text into sections In-Reply-To: References: X-Mailer: Claws Mail 3.8.1 (GTK+ 2.24.10; x86_64-pc-linux-gnu) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Get-Message-Sender-Via: boston.accountservergroup.com: authenticated_id: tim@thechases.com Cc: python-list@python.org 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1398531613 news.xs4all.nl 2968 [2001:888:2000:d::a6]:40733 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70637 On 2014-04-26 23:53, oyster wrote: > 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. Your follow-up reply made much more sense and your written English is far better than many native speakers'. :-) > 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. I suspect you have a minimum number of characters (or words) to consider, otherwise a single character duplicated at the beginning of the line would delimit a section, such as abcd afgh because they share the commonality of an "a". The code I provided earlier should give you what you describe. I've tweaked and tested, and provided it below. Note that I require a minimum overlap of 6 characters (MIN_LEN). It also gathers the initial stuff (that you want to discard) under the empty key, so you can either delete that, or ignore it. > I need a method to split the whole text into SECTIONs and to know > all the KEYs > > I have tried to solve this problem via re module I don't think the re module will be as much help here. -tkc from collections import defaultdict import itertools as it MIN_LEN = 6 def overlap(s1, s2): "Given 2 strings, return the initial overlap between them" return ''.join( c1 for c1, c2 in it.takewhile( lambda pair: pair[0] == pair[1], it.izip(s1, s2) ) ) prevline = "" # the initial key under which preamble gets stored output = defaultdict(list) key = None with open("data.txt") as f: for line in f: if len(line) >= MIN_LEN and prevline[:MIN_LEN] == line[:MIN_LEN]: key = overlap(prevline, line) output[key].append(line) prevline = line for k,v in output.items(): print str(k).center(60,'=') print ''.join(v) .