简体   繁体   中英

Accessing values from a Unicode dictionary in Python

I have Unicode values similar to this in a dictionary:

{u'id': u'100000000265946', u'name': u'Sophia N Art Fuentes'}
{u'id': u'100000538132142', u'name': u'Tatiana Vargas'}
{u'id': u'1669912701', u'name': u'Milvia Albanez'}

I need to access keys and values but I'm getting this error

AttributeError: 'unicode' object has no attribute 'keys'

I am using Python 2.7. Is there any method to convert Unicode to ASCII? Or how do I access the values as Unicode itself?

>>> s = u"{u'id': u'100000000265946', u'name': u'Sophia N Art Fuentes'}"
>>> s.keys()

Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    s.keys()
AttributeError: 'unicode' object has no attribute 'keys'
>>> import ast
>>> d = ast.literal_eval(s)
>>> d.keys()
[u'id', u'name']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM