Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.014 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'received:209.85.223': 0.03; '"""': 0.05; 'expressions': 0.07; 'python': 0.09; 'iterate': 0.09; 'methods,': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr :python-list': 0.10; '"values"': 0.16; "'0',": 0.16; '0.2': 0.16; '0.3': 0.16; '204': 0.16; 'csv': 0.16; 'subject:File': 0.16; 'subject:Text': 0.16; 'wrote:': 0.17; 'module': 0.19; 'file.': 0.20; 'trying': 0.21; 'parse': 0.22; 'work.': 0.23; 'monday,': 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'values': 0.26; 'cc:addr:gmail.com': 0.27; '3.0': 0.27; 'format,': 0.27; 'regular': 0.27; 'lines': 0.28; 'spaces': 0.29; 'url:python': 0.32; 'file': 0.32; 'could': 0.32; 'print': 0.32; 'extract': 0.33; 'values.': 0.33; 'hi,': 0.33; 'received:google.com': 0.34; 'text': 0.34; 'list': 0.35; 'table': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'anything': 0.36; 'test': 0.36; 'should': 0.36; 'possible': 0.37; 'best,': 0.37; 'received:209': 0.37; 'well.': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'url:docs': 0.38; 'help': 0.40; 'end': 0.40; 'your': 0.60; 'traffic': 0.61; 'kind': 0.61; 'here:': 0.62; 'different': 0.63; 'august': 0.66; "'1.0',": 0.84; "'3.0',": 0.84; '0000': 0.84; '144': 0.84; 'fin': 0.84; 'nagy': 0.84; '6.4': 0.91; 'results,': 0.91; 'subject:From': 0.97 Newsgroups: comp.lang.python Date: Wed, 5 Sep 2012 06:08:04 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=122.167.254.242; posting-account=uPFZNQoAAAAm9w7z13q1SjWNKNjztdcD References: <481dc39d-1dee-4ebe-97d5-ccad659f8c74@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 122.167.254.242 MIME-Version: 1.0 Subject: Re: Extract Text Table From File From: Ramchandra Apte To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: Huso , 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: , Message-ID: Lines: 134 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346850488 news.xs4all.nl 6926 [2001:888:2000:d::a6]:58778 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:28490 On Monday, 27 August 2012 15:42:14 UTC+5:30, Laszlo Nagy wrote: > On 2012-08-27 11:53, Huso wrote: > > > Hi, > > > > > > I am trying to extract some text table data from a log file. I am trying different methods, but I don't seem to get anything to work. I am kind of new to python as well. Hence, appreciate if someone could help me out. > > > > # > > # Write test data to test.txt > > # > > > > data = """ > > ROUTES TRAFFIC RESULTS, LSR > > TRG MP DATE TIME > > 37 17 120824 0000 > > > > R TRAFF NBIDS CCONG NDV ANBLO MHTIME NBANSW > > AABBCCO 6.4 204 0.0 115 1.0 113.4 144 > > AABBCCI 3.0 293 115 1.0 37.0 171 > > DDEEFFO 0.2 5 0.0 59 0.0 107.6 3 > > EEFFEEI 0.0 0 59 0.0 0.0 0 > > HHGGFFO 0.0 0 0.0 30 0.0 0.0 0 > > HHGGFFI 0.3 15 30 0.0 62.2 4 > > END > > """ > > fout = open("test.txt","wb+") > > fout.write(data) > > fout.close() > > > > # > > # This is how you iterate over a file and process its lines > > # > > fin = open("test.txt","r") > > for line in fin: > > # This is one possible way to extract values. > > values = line.strip().split() > > print values > > > > > > This will print: > > > > [] > > ['ROUTES', 'TRAFFIC', 'RESULTS,', 'LSR'] > > ['TRG', 'MP', 'DATE', 'TIME'] > > ['37', '17', '120824', '0000'] > > [] > > ['R', 'TRAFF', 'NBIDS', 'CCONG', 'NDV', 'ANBLO', 'MHTIME', 'NBANSW'] > > ['AABBCCO', '6.4', '204', '0.0', '115', '1.0', '113.4', '144'] > > ['AABBCCI', '3.0', '293', '115', '1.0', '37.0', '171'] > > ['DDEEFFO', '0.2', '5', '0.0', '59', '0.0', '107.6', '3'] > > ['EEFFEEI', '0.0', '0', '59', '0.0', '0.0', '0'] > > ['HHGGFFO', '0.0', '0', '0.0', '30', '0.0', '0.0', '0'] > > ['HHGGFFI', '0.3', '15', '30', '0.0', '62.2', '4'] > > ['END'] > > > > > > The "values" list in the last line contains these values. This will work > > only if you don't have spaces in your values. Otherwise you can use > > regular expressions to parse a line. See here: > > > > http://docs.python.org/library/re.html > > > the csv module should be used for this not regex > Since you did not give any specification on your file format, it would > > be hard to give a concrete program that parses your file(s) > > > > Best, > > > > Laszlo