data parsing
Mike Fletcher
mfletch at tpresence.com
Fri Feb 23 17:23:57 EST 2001
Here's some ideas (untested, of course):
import string
lines = open( filename ).readlines()
records = map( string.split, lines, ['|']*len(lines))
results = []
while records:
currentresult = [ string.join(records[0], ';')]
del records[0]
while records and not records[0][0]:
currentresult.append( string.join(records[0], ';') )
del records[0]
results.append( string.join( currentresult, '|'))
open( newfilename, 'w').write( string.join( results, '\n'))
Try that in the interactive interpreter to get a feel for what's happening.
Hopefully it'll get you started. Good luck,
Mike
-----Original Message-----
From: Gnanasekaran Thoppae [mailto:gnana at mips.biochem.mpg.de]
Sent: Friday, February 23, 2001 12:42 PM
To: python-list at cwi.nl
Subject: data parsing
Hi,
I have some data in a file 'test', which contains:
Joe|25|30|49|40|
|28|39|71||
|30|29|||
Malcolm|43|60|56||
|28|37|||
Amy||70|45||
|40|30||40
|40||30||
This is basically a multi line (record) values that belong
to the first line that starts with a filled field.
This is part belongs to joe 'Joe'
Joe|25|30|49|40|
|28|39|71||
|30|29|||
This to 'Malcolm'
Malcolm|43|60|56||
|28|37|||
and the rest to Amy.
I want to parse this data and format it in this way:
Joe|25;28;30|30;39;29|49;71|40|
Malcolm|43;28|60;37|56||
Amy|40;40|70;30|45;;30|;40;|
Basically speaking, I am trying to cluster multi record
data into one field, each seperated by a delimiter ';' and
if the field is empty, an empty ; will enable later on to
decode the field as empty field ''.
Thanks.
-gnana
--
http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list