Parse variable into variables >Re: Apology

Laura Creighton lac at strakt.com
Mon Feb 25 04:42:51 EST 2002


> 
> I can read a line from a file but then I want to parse the line into a set
> of variables. How do I do the parse?
> 
> If I have to do this a baby step at a time then so be it.

Somebody recommended regular expressions.  They are quite difficult to
use.  Don't use them unless you really need to -- and most of the time
python programmers do not need to.

If by 'parse the lines into a set of variables' you mean 'split
the lines into whitespace separated words' then this will work for you.

lac at ratthing-b246:~/scratch$ python
Python 2.1.2 (#1, Jan 18 2002, 18:05:45)
[GCC 2.95.4  (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> line='a b c d e f g h i jj kk ll mm nnn ooo ppp qqq rrr ssss tttt'
>>> print line
a b c d e f g h i jj kk ll mm nnn ooo ppp qqq rrr ssss tttt
>>> words=line.split()
>>> print words
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'jj', 'kk', 'll', 'mm', 'nnn', 'ooo', 'ppp', 'qqq', 'rrr', 'ssss', 'tttt']
>>> print words[0], words[6]
a g
>>>
-------------
Happy hacking,
Laura Creighton




More information about the Python-list mailing list