Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27971

Re: Extract Text Table From File

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 <gandalf@shopzeus.com>
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 <gandalf@shopzeus.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0
MIME-Version 1.0
To Huso <hussain.a.rasheed@gmail.com>, 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 <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3865.1346062345.4697.python-list@python.org> (permalink)
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

Show key headers only | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Extract Text Table From File Huso <hussain.a.rasheed@gmail.com> - 2012-08-27 02:53 -0700
  Re: Extract Text Table From File Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-27 12:12 +0200
    Re: Extract Text Table From File Huso <hussain.a.rasheed@gmail.com> - 2012-08-27 03:34 -0700
    Re: Extract Text Table From File Huso <hussain.a.rasheed@gmail.com> - 2012-08-27 03:34 -0700
      Re: Extract Text Table From File Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-27 13:07 +0200
    Re: Extract Text Table From File Huso <hussain.a.rasheed@gmail.com> - 2012-08-27 04:23 -0700
    Re: Extract Text Table From File Huso <hussain.a.rasheed@gmail.com> - 2012-08-27 04:23 -0700
      Re: Extract Text Table From File Laszlo Nagy <gandalf@shopzeus.com> - 2012-08-27 13:55 +0200
    Re: Extract Text Table From File Ramchandra Apte <maniandram01@gmail.com> - 2012-09-05 06:08 -0700
    Re: Extract Text Table From File Tim Chase <python.list@tim.thechases.com> - 2012-09-05 11:25 -0500
  Re: Extract Text Table From File Tim Chase <python.list@tim.thechases.com> - 2012-08-27 07:42 -0500

csiph-web