Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'algorithm': 0.03; 'exception': 0.03; 'lines.': 0.07; 'subject:file': 0.07; 'try:': 0.07; 'python': 0.09; '"r")': 0.09; "'''": 0.09; 'skip:# 30': 0.09; 'toggle': 0.09; 'skip:# 20': 0.13; '0")': 0.16; 'ack': 0.16; 'err,': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'iterating': 0.16; 'line)': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'pprint': 0.16; 're,': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'uniq': 0.16; 'wrote:': 0.17; 'string,': 0.17; 'unicode': 0.17; 'skip:" 30': 0.20; 'import': 0.21; 'example': 0.23; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:# 10': 0.27; 'set.': 0.27; 'skip:e 40': 0.27; "doesn't": 0.28; 'lines': 0.28; 'container': 0.29; 'received:192.168.1.3': 0.29; 'searches': 0.29; 'yields': 0.29; 'objects': 0.29; 'starts': 0.29; 'received:84': 0.32; 'print': 0.32; 'anywhere': 0.33; 'to:addr:python-list': 0.33; 'wrong': 0.34; 'done': 0.34; 'fail': 0.35; 'false': 0.35; 'path': 0.35; 'there': 0.35; 'really': 0.36; 'except': 0.36; 'but': 0.36; 'enough': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'daily': 0.38; 'files': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'july': 0.60; 'skip:u 10': 0.60; 'real': 0.61; 'save': 0.61; 'free': 0.61; 'interest': 0.62; 'close': 0.63; 'within': 0.64; 'note:': 0.64; 'therefore': 0.65; 'treat': 0.65; 'header:Reply-To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'here!': 0.84; 'isolate': 0.84; 'reply-to:addr:python.org': 0.84; 'subject:over': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=FLuZNpUs c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=DKcI9XZsuF4A:10 a=9kPJSUpzQ7UA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=C2KnmdSWxSdxPC3K-jIA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Tue, 24 Jul 2012 13:10:45 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: groveling over a file for Q:: and A:: stmts References: <4851867b-6c8e-41e4-8d39-56e10e193a12@googlegroups.com> In-Reply-To: <4851867b-6c8e-41e4-8d39-56e10e193a12@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org 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: 94 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1343131847 news.xs4all.nl 6840 [2001:888:2000:d::a6]:46915 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:25980 On 24/07/2012 08:50, paul618 wrote: > #!/usr/bin/env python > # grep_for_QA.py I am only looking to isolate uniq Q:: and A:: stmts from my daily files > # > # note: This algorithm will fail if there are any blank lines within the Q and A area of interest (a paragraph) > > # D. Beazley is my fav documentation > > import re, glob > import pprint as pp > > sampledata = ''' > A:: And Straight Street is playin on the Radio Free Tibet. What are the chances, DTMB? > Q:: About 1 in 518400, Professor. > A:: Correct! Err, I thought it was 1:410400, but close enough for jazz! > > > ''' > > pattern0 = re.compile("Q::") > pattern1 = re.compile("A::") # objects of interest can start with A:: ;; not alway Q:: > END_OF_PARAGRAPH_pat = "\n\s*\n" > > path = "/Users/paultaney/dailies2012/0722" # an example of real data set. > > toggle = False > L = [] > M = [] > > #file = open(path, "r") > try: > #for line in file.readlines(): > for line in sampledata: sampledata is a string, therefore this is iterating over the string, which yields characters, not lines. Try using sampledata.splitlines(): for line in sampledata.splitlines(): > try: > # Later, I also need to treat Unicode -- and I am clueless. > > # falsestarts:: > #line.encode("utf8").decode('xxx', 'ignore') > #line.encode("utf8", 'ignore') > #line.decode('8859') > #line.decode('8859') # 8859, Latin-1 doesn't cover my CJK pastings AT ALL > #line.decode('GB18030') # 171006 -- ack > #encoded_line = line # xxx line.encode("utf8") > > mo0 = re.search(pattern0, line) This searches for pattern0 anywhere in the line. You really want to check whether the line starts with pattern0, which is better done with: line.startswith("Q::") > mo1 = re.search(pattern1, line) > mo2 = re.search(END_OF_PARAGRAPH_pat, line) > > if mo0: > if 1: print ("I see pattern 0") > toggle = True > if 1: print(line) > M.append(mo0.group()) > > if mo1: > if 1: print ("I see pattern 1") > toggle = True > M.append(mo1.group()) > > if mo2 and toggle: > if 1: print ("I see pattern 2 AND toggle is set") > # got one. save it for uniqifying, and empty the container > toggle = False > L.append(M) > M = [] > > except Exception as e: > print("--- " + e + " ---") > > except UnicodeDecodeError: > #encoded_line = encoded_line.urlsafe_b64encode(re.replace("asdf", encoded_line)) > #line = re.sub(".+", "--- asdf ---", line) > pass > > L.sort > print (L) > > # and what"s wrong with some of this, here! > #myHash = set(L) # uniqify > #pp.pprint(myHash) # july 23, 131001 hike! >