Extract Text Table From File

Huso hussain.a.rasheed at gmail.com
Mon Aug 27 06:34:03 EDT 2012


On Monday, August 27, 2012 3:12:14 PM UTC+5, 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
> 
> 
> 
> 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

Hi,

Thank you for the information.
The exact way I want to extract the data is like as below.

TRG, MP and DATE and TIME is common for that certain block of traffic.
So I am using those and dumping it with the rest of the data into sql.
Table will have all headers (TRG, MP, DATE, TIME, R, TRAFF, NBIDS, CCONG, NDV, ANBLO, MHTIME, NBANSW).

So from this text, the first data will be 37, 17, 120824, 0000, AABBCCO, 6.4, 204, 0.0, 115, 1.0, 113.4, 144.

Thanking,
Huso



More information about the Python-list mailing list