Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Extract Text Table From File

Reply
Thread Tools

Extract Text Table From File

 
 
Ramchandra Apte
Guest
Posts: n/a
 
      09-05-2012
On Monday, 27 August 2012 15:42:14 UTC+5:30, 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
>
>
>

the csv module should be used for this not regex
> 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


 
Reply With Quote
 
 
 
 
Tim Chase
Guest
Posts: n/a
 
      09-05-2012
[trimming out a bunch of superfluous text so the thread is actually
readable]

On 09/05/12 08:08, Ramchandra Apte wrote:
> On Monday, 27 August 2012 15:42:14 UTC+5:30, Laszlo Nagy wrote:
>> On 2012-08-27 11:53, Huso wrote:
>>> I am trying to extract some text table data from a log file

>>
>> fin = open("test.txt","r")
>>
>> for line in fin:
>>
>> # This is one possible way to extract values.
>>
>> values = line.strip().split()
>>
>> print values

>
> the csv module should be used for this not regex


The problem is that the csv module expects a single delimiter
character, not columnar data.

-tkc


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extract Text Format Table Data hussain.a.rasheed@gmail.com Python 0 08-27-2012 09:45 AM
Please help me how is easiest way to extract text between some variable text Mladen Perl Misc 5 02-22-2011 10:57 AM
How do i extract vidios when winrar wont extract them??? help plzzzzzzzz smuttdog@sc.rr.com Computer Support 2 12-23-2007 07:03 AM
extract table from xhtml file and java Damo_Suzuki Java 0 12-09-2006 04:44 PM
Extract a portion from a line of a text-file Prabh Java 1 09-01-2003 10:50 PM



Advertisments