Breaking String into Values
Gustavo Cordova
gcordova at hebmex.com
Fri Mar 29 17:20:16 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
>
How about using a regular expression?
>>> import sre
>>> pattern = sre.compile(r"(\d{5})(\d{5})(\d{6})(.)\s+(\S+)\s+(\d{2})")
>>> match = pattern.match("3500035000010104A Foo 45")
>>> match.groups()
('35000', '35000', '010104', 'A', 'Foo', '45')
>>>
And, with the match object, you can do a multiple assignment:
>>> a, b, c, d, e, f = match.groups()
>>> print a, b, c, d, e, f
35000 35000 010104 A Foo 45
>>>
Good luck!
-gustavo
More information about the Python-list
mailing list