Breaking String into Values

robert garretson wright rgwright at ux.cso.uiuc.edu
Fri Mar 29 17:10:47 EST 2002


I am working on reading in a data file format which is set up as a series
of lines that look like this:

3500035000010104A Foo 45

I want to break up into a variables as follows:
a = 35000, b = 35000, c = 10104, d = 'A', e = 'Foo', f = 45

My current code (auto-generated from a data dictionary) looks
something like this:

temp = line[0:5]
a = int(temp)
temp = line[5:10]
b = int(temp)
temp = line[10:15]
c = int(temp)
temp = line[15:16]
d = temp
temp = line[16:20]
temp = temp.rstrip()
e = temp
temp = line[20:23]
f = int(temp)

with a bit more error checking around the int() calls.

Is there a better way to do this? The files have around 1000-8000 lines each
so I would like it to be fast. Is there a package around that someone has
coded up as a C-extension to do this?

-- 
Robert Wright
rgwright at uiuc.edu



More information about the Python-list mailing list