NEWB: how to convert a string to dict (dictionary)
Jorge Godoy
godoy at ieee.org
Wed May 24 02:10:49 EDT 2006
manstey wrote:
> Hi,
>
> How do I convert a string like:
> a="{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}"
>
> into a dictionary:
> b={'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}
>
> Thanks, Matthew
>
> PS why in Python is it so often easy to convert one way but not the
> other?
I don't belive it is Python's fault. In fact, you're going from a data
structure that contains enough information to be converted to a string in
one way -- dict to string -- while you're going from another data structure
that has no information at all about its contents on the other way --
string to dict/list/whatever.
Anyway:
In [1]:a="{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS':
u'8'}"
In [2]:type(a)
Out[2]:<type 'str'>
In [3]:b = eval(a)
In [4]:type(b)
Out[4]:<type 'dict'>
In [5]:b
Out[5]:{'syllable': u'cv-i b.v^ y^-f', 'ketiv-qere': 'n', 'wordWTS': u'8'}
Be seeing you,
--
Jorge Godoy <godoy at ieee.org>
"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
More information about the Python-list
mailing list