Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'subject:text': 0.05; 'column': 0.07; 'subject:file': 0.07; 'subject:help': 0.08; 'string': 0.09; 'ambiguity': 0.09; 'append': 0.09; 'delimited': 0.09; 'level:': 0.09; 'sure,': 0.09; 'python': 0.11; 'useful,': 0.14; '23,': 0.16; 'order?': 0.16; 'substring': 0.16; 'tuple,': 0.16; 'wrote:': 0.18; '(where': 0.19; 'possible,': 0.19; 'split': 0.19; 'starts': 0.20; 'example': 0.22; 'header:User-Agent:1': 0.23; 'error': 0.23; 'parse': 0.24; 'skip': 0.24; 'string,': 0.24; 'file.': 0.24; 'looks': 0.24; 'second': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'record': 0.27; 'point': 0.28; 'lines': 0.31; '25,': 0.31; 'extract': 0.31; 'file': 0.32; 'another': 0.32; 'text': 0.33; 'open': 0.33; 'checking': 0.33; 'subject:with': 0.35; 'something': 0.35; 'but': 0.35; 'really': 0.36; 'done,': 0.36; 'like,': 0.36; 'words,': 0.36; 'next': 0.36; 'hi,': 0.36; 'level': 0.37; 'list.': 0.37; 'starting': 0.37; 'represent': 0.38; 'whatever': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'space': 0.40; 'how': 0.40; 'solve': 0.60; 'till': 0.61; 'first': 0.61; 'save': 0.62; 'field': 0.63; 'happen': 0.63; 'more': 0.64; 'sample': 0.67; 'line,': 0.68; 'on...': 0.68; 'received:74.208': 0.68; 'useful.': 0.68; 'repeat': 0.74; 'score': 0.74; 'irrelevant': 0.84; 'round,': 0.91 Date: Fri, 05 Dec 2014 00:46:04 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: help with processing text file References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:cL8/9yPPadN8Ihr/D+6JeL6VI+JVMILvtChANRGITIJ LTmlY9BQTDxy0TSqx8DsC8yOu9yK+OxVa0+RZjyZKIt3ZDIHDS sU6VIfpY84Kf1K+3hNP5fgZt638CuZYGjxgKxfm//CzDj1IArO w4PHdtnqgCDg9Qdtt3kju/al3sfhGXDnVLT32rQYSUovxK1ap+ Ojp+dgP9X/ugRuWtL4GyuPLnfKcXNlYulG/bnYji7DdAeFV+Ch Mnr09O2irKiKOjy27bMz1Vq/PNnRvsKbZuoeYc5mryqZuxTuS5 dyhuqOVkLaHSw3GcU5Hb4ofV/KQxuzqV17qyHDtUQKWiBERve3 vAmeyWz5xopBhyXv4XP4= X-UI-Out-Filterresults: notjunk:1; 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: 55 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417758377 news.xs4all.nl 2848 [2001:888:2000:d::a6]:42832 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81977 On 12/04/2014 11:46 PM, C. Ng wrote: > Hi, > > Given the sample text file below (where the gibberish represent the irrelevant portions) : > > .... > abcddsdfffgfg > ggfhghghgfhghgh round 5 xccdcxcfd > sdfdffdfbcvcvbbvnghg score = 0.4533 > abcddsdfffgfg round 5 level = 0.15 > ggfhghghgfhghgh round 10 dfsdfdcdsd > sdfdffdfbcvcvbbvnghg score = 0.4213 > sdsdaawddddsds round 10 level = 0.13 > ......and so on.... > > > I would like to extract the values for round, score and level: > 5 0.4533 0.15 > 10 0.4213 0.13 > ....and so on... > > Please advise me how it can be done, and what Python functions are useful. > There's lots of ambiguity in that "specification." Can you be sure, for example that the gibberish does not ever include the string "round", "score", or "level"? Can you be sure that the relevant 3 lines for a given record are adjacent, and in that order? Do you happen to know that "round" always starts in a particular column?, and that "score" starts in another particular column? How would you solve it by hand? Something like the following? OPen the file. Skip all lines till column 19-23 contain "round" find the first space delimited field starting in column 25, and call it round_num On the next line, split the line into words, and save the last word into score_val On the next line, take a substring of the line starting with column 23, parse it into words, and store the second word in level_num Save the values round_num,score_val, and level_num in a tuple, or a string, or whatever you find useful, and append it to a result list. Repeat till end of file. Lots more error checking is possible, and advisable, but without knowing what the file really looks like, I see no point in guessing. -- DaveA