Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feed.xsnews.nl!border-1.ams.xsnews.nl!newsfeed.xs4all.nl!newsfeed5.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.054 X-Spam-Evidence: '*H*': 0.89; '*S*': 0.00; '"""': 0.05; 'expressions': 0.07; 'python': 0.09; 'iterate': 0.09; 'methods,': 0.09; '"values"': 0.16; "'0',": 0.16; '0.2': 0.16; '0.3': 0.16; '204': 0.16; 'subject:File': 0.16; 'subject:Text': 0.16; 'wrote:': 0.17; 'file.': 0.20; 'trying': 0.21; 'parse': 0.22; 'work.': 0.23; 'to:2**1': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'values': 0.26; '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; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'text': 0.34; 'list': 0.35; 'table': 0.35; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'anything': 0.36; 'test': 0.36; 'possible': 0.37; 'best,': 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; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'help': 0.40; 'end': 0.40; 'your': 0.60; 'traffic': 0.61; 'kind': 0.61; 'here:': 0.62; 'different': 0.63; 'received:204': 0.72; "'1.0',": 0.84; "'3.0',": 0.84; '0000': 0.84; '144': 0.84; 'fin': 0.84; '6.4': 0.91; 'results,': 0.91; 'subject:From': 0.97 Date: Mon, 27 Aug 2012 12:12:14 +0200 From: Laszlo Nagy User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Huso , python-list@python.org Subject: Re: Extract Text Table From File References: <481dc39d-1dee-4ebe-97d5-ccad659f8c74@googlegroups.com> In-Reply-To: <481dc39d-1dee-4ebe-97d5-ccad659f8c74@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 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: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1346062345 news.xs4all.nl 6960 [2001:888:2000:d::a6]:39298 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:27971 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 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