Extract Text Table From File
Tim Chase
python.list at tim.thechases.com
Mon Aug 27 08:42:52 EDT 2012
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
More information about the Python-list
mailing list