简体   繁体   English

在Python中从Unicode字典访问值

[英]Accessing values from a Unicode dictionary in Python

I have Unicode values similar to this in a dictionary: 我在字典中有与此类似的Unicode值:

{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' AttributeError:“ unicode”对象没有属性“ keys”

I am using Python 2.7. 我正在使用Python 2.7。 Is there any method to convert Unicode to ASCII? 有什么方法可以将Unicode转换为ASCII? Or how do I access the values as Unicode itself? 或者我如何以Unicode本身的形式访问这些值?

>>> 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']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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