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


Groups > comp.lang.python > #27982

Re: Extract Text Table From File

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'iterate': 0.09; 'throw': 0.09; 'cc:addr:python-list': 0.10; 'files.': 0.13; 'skip:f 30': 0.15; '-tkc': 0.16; '0.2': 0.16; '0.3': 0.16; '204': 0.16; 'columns': 0.16; 'csv': 0.16; 'dict(': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'headers:': 0.16; 'message- id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'row': 0.16; 'subject:File': 0.16; 'subject:Text': 0.16; 'wrote:': 0.17; 'headers': 0.17; 'file.': 0.20; 'import': 0.21; 'cc:2**0': 0.23; "i've": 0.23; 'cc:no real name:2**0': 0.24; 'header': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header :User-Agent:1': 0.26; 'values': 0.26; '3.0': 0.27; 'rest': 0.28; 'spaces': 0.29; 'case,': 0.29; 'read,': 0.29; 'code': 0.31; 'that,': 0.34; "won't": 0.35; 'something': 0.35; 'there': 0.35; 'data.': 0.36; 'data': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'build': 0.39; 'end': 0.40; 'your': 0.60; 'easy': 0.60; 'map': 0.61; 'traffic': 0.61; 'different': 0.63; 'more': 0.63; 'forward': 0.66; '0000': 0.84; '144': 0.84; 'received:50.22': 0.84; '6.4': 0.91; 'results,': 0.91; 'subject:From': 0.97
Date Mon, 27 Aug 2012 07:42:52 -0500
From Tim Chase <python.list@tim.thechases.com>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111120 Icedove/3.1.16
MIME-Version 1.0
To Huso <hussain.a.rasheed@gmail.com>
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
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - boston.accountservergroup.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - tim.thechases.com
X-Source
X-Source-Args
X-Source-Dir
Cc python-list@python.org
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.3874.1346071301.4697.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1346071301 news.xs4all.nl 6894 [2001:888:2000:d::a6]:53233
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27982

Show key headers only | View raw


On 08/27/12 04:53, Huso wrote:
> Below is just ONE block of the traffic i have in the log files. There will be more in them with different 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
> HHGGFFI     0.3      15            30    0.0     62.2       4
> END

In the past I've used something like the following to find columnar
data based on some found headers:

  import re
  token_re = re.compile(r'\b(\w+)\s*')
  f = file(FILENAME)
  headers = f.next() # in your case, you'd
                     # search forward until
                     # you got to a header line
                     # and use that TRAFF... line
  header_map = dict(
    # build a map of field-name to slice
    (
      matchobj.group(1).upper(),
      slice(*matchobj.span())
    )
    for matchobj
    in token_re.finditer(headers)
    )

You can then access your values as you iterate through the rest of
the rows:

  for row in f:
    if row.startswith("END"): break
    traff = float(row[header_map["TRAFF"]])
    # ...

which makes the code pretty easy to read, effectively turning it
into a CSV file.

It has the advantage that, if for some reason data in the columns
have spaces in them, it won't throw off the row as a .split() would.

-tkc


Back to comp.lang.python | Previous | NextPrevious 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