[Tutor] Interpret the contents of a line
Makarand Datar
datar at wisc.edu
Thu Jul 11 00:12:16 CEST 2013
Hi,
I am working on a parser and my input file is a description about bunch of
things written line by line. So for instance, consider the following two
lines and what the corresponding variables in python should look like.
example Line1: position = 1, 1, rotation = 90, 0, 0, mass = 120; Here I
want the variables to be position = [1,1,0], rotation = [90,0,0], mass =
[120]
example Line2: position = 1, 1, 2, mass = 120, rotation = 90, 0; Here I
want the variables to be position = [1,1,2], rotation = [90,0,0], mass =
[120]
example Line3: mass = 120, rotation = 90, 0; Here I want the variables to
be position = [0,0,0], rotation = [90,0,0], mass = [120]
I know the maximum number of arguments possible for each variable. For
example, in the first line above, only two numbers for position ares
specified; that means that the third entry in position list is zero. So I
need to handle these cases while reading in the file as well. Additionally,
the text might not always be in the same order; like shown in the second
line above. And finally, sometimes numbers for one variable might not exist
at all; like shown in line 3. Here, the position is then read as position =
[0,0,0].
How do I implement such stuff? Is there some smart way of doing this? All I
have in mind is this: read a line as comma or a space delimited list of
words variable and then some how use if else + len(list) to figure out
whats going on. But that seems way too tedious and I feel that there might
be an easier way to read such things.
Any help is highly appreciated.
Thank you
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130710/109949e5/attachment-0001.html>
More information about the Tutor
mailing list