Breaking String into Values
Chris Liechti
cliechti at gmx.net
Fri Mar 29 18:09:09 EST 2002
robert garretson wright <rgwright at ux.cso.uiuc.edu> wrote in
news:HD5p8.33836$tg4.398811 at vixen.cso.uiuc.edu:
> 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?
i had to remove one "0" from the input to match you results above
here's some abuse of the struct module
>>> l = struct.unpack("5s5s5s1s4s3s","350003500010104A Foo 45")
>>> a,b,c = map(int, l[:3])
>>> d,e = l[3:5]
>>> f = int(l[5])
>>> a,b,c,d,e,f
(35000, 35000, 10104, 'A', ' Foo', 45)
but i think your solution is easy to understand and fast enough for
some thousand lines within seconds.
chris
--
Chris <cliechti at gmx.net>
More information about the Python-list
mailing list