Line Text Parsing

wes weston wweston at att.net
Wed Feb 4 16:09:58 EST 2004



allanc wrote:
> I'm new with python so bear with me.
> 
> I'm looking for a way to elegantly parse fixed-width text data (as opposed 
> to CSV) and saving the parsed data unto a database. The text data comes 
> from an old ISAM-format table and each line may be a different record 
> structure depending on key fields in the line.
> 
> RegExp with match and split are of interest but it's been too long since 
> I've dabbled with RE to be able to judge whether its use will make the 
> problem more complex.
> 
> Here's a sample of the records I need to parse:
> 
> 01508390019002      11284361000002SUGARPLUM
> 015083915549           SHORT ON LAST ORDER 
> 0150839220692 000002EA BMC   15 KG   001400
> 
> 1st Line is a (portion of) header record.
> 2nd Line is an text instruction record.
> 3rd Line is a Transaction Line Item record.
> 
> Each type of record has a different structure. But these set of lines 
> appear in the one table.
> 
> 
> Any ideas would be greatly appreciated.
> 
> Allan

Allan,
     Maybe this will help more:

 >>> line = "015083915549           SHORT ON LAST ORDER 0150839220692"
 >>> print line[0:10]
0150839155
 >>> print line [:10]
0150839155
 >>> print line[5:10]
39155
 >>> print line[-10:-1]
083922069
 >>> print int(line[-10:-1])
83922069
 >>> print "  xyz  ".strip()
xyz

wes




More information about the Python-list mailing list