Line Text Parsing

wes weston wweston at att.net
Wed Feb 4 15:55:11 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


allanc,
     -slices as in str[0:5] or str[5:] or str[5:-1] - get pieces of a string
     -you'll probably want to strip leading/trailing spaces; see strings doc
     -you may need to cast/convert
     _int = int("55")
     _float = float("4.2")
wes




More information about the Python-list mailing list