Question about dictionary and graph?
Terry Reedy
tjreedy at udel.edu
Wed Nov 6 15:09:06 EST 2002
"Mindy" <csshi99 at yahoo.com> wrote in message
news:mailman.1036608704.18103.python-list at python.org...
> I have a string like "from=string1,to=string2,symbole=
> 'string3' ", I want to parse it
>>> s="from=string1,to=string2,symbol=string3"
>>> d={}
>>> fields = s.split(',')
>>> for f in fields:
... k,v = f.split('=')
... d[k] = v
...
>>> print d
{'to': 'string2', 'symbol': 'string3', 'from': 'string1'}
>and place the three fields into a tuple (from,to ,symbol).
If you *know* fields are always in same order (in which case, field
names are *not* needed), you can construct tuple in fields loop
instead of dict. Otherwise,
>>> tup = (d['from'], d['to'],d['symbol'])
> I also want to stick an ID(just an integer) to each tuple.
Trivial exercise for you
> So how to parse this? And is it okay to use m.groupdict() to get
> a dictionary from the results of parsing?
With string methods instead of re's.
> And still another question, if I want to draw a line
> with arrow pointed from "from" to "to", how can I do
> this? I think this question is about how to draw a
> graph in Python. Thanks for any hints!
Did you try google? ('Python graph')
Terry J. Reedy
More information about the Python-list
mailing list