mas*_*don 4 python django unicode encoding dictionary
我有来自facebook的朋友列表的示例回复:
[{u'uid': 513351886, u'name': u'Mohammed Hossein', u'pic_small': u'http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs643.snc3/27383_513351886_4933_t.jpg'},
{u'uid': 516583220, u'name': u'Sim Salabim', u'pic_small': u'http://profile.ak.fbcdn.net/hprofile-ak-snc4/hs348.snc4/41505_516583220_5681339_t.jpg'}]
Run Code Online (Sandbox Code Playgroud)
我如何解析这个列表编码字典的关键字到ascii?我尝试过这样的事情:
response = simplejson.load(urllib.urlopen(REST_SERVER, data))
for k in response:
for id, stuff in k.items():
id.encode("ascii")
logging.debug("id: %s" % id)
return response
Run Code Online (Sandbox Code Playgroud)
但是编码的密钥没有保存,因此我仍然得到unicode值.
Kar*_*tel 10
第一:你真的需要这样做吗?这些字符串是Unicode的原因:您根本无法用Unicode表示纯ASCII格式的所有内容.对于你的字典键'uid','name'和'pic_small'来说,这可能不会有问题; 但是将它们保留为Unicode可能也不会成为问题.('simplejson'库对您的数据一无所知,因此它为每个字符串使用Unicode - 比抱歉更安全.)
无论如何:
在Python中,不能修改字符串.该.encode方法不会更改字符串; 它返回一个新的字符串,它是编码版本.
你想要做的是产生一个新的字典,用密码替换密钥.我们可以通过将每对(编码键,原始值)作为*args传递给dict构造函数来完成此操作.
看起来像:
dict((k.encode('ascii'), v) for (k, v) in original.items())
Run Code Online (Sandbox Code Playgroud)
类似地,我们可以使用列表推导将其应用于每个字典,并创建新列表.(我们可以就地修改列表,但这种方式更清晰.)
response = simplejson.load(urllib.urlopen(REST_SERVER, data))
# We create the list of modified dictionaries, and re-assign 'response' to it:
response = [
dict((k.encode('ascii'), v) for (k, v) in original.items()) # the modified version
for original in response # of each original dictionary.
]
return response
Run Code Online (Sandbox Code Playgroud)
你的其他回复暗示了这一点,但是没有说出来:Python中的字典查找和字符串比较在Unicode和ASCII之间透明地转换:
>>> x = {u'foo':'bar'} # unicode key, ascii value
>>> x['foo'] # look up by ascii
'bar'
>>> x[u'foo'] # or by unicode
'bar'
>>> x['foo'] == u'bar' # ascii value has a unicode equivalent
True
Run Code Online (Sandbox Code Playgroud)
因此,对于从JSON转换的字典的大多数用法,您通常不必担心所有内容都是Unicode.
| 归档时间: |
|
| 查看次数: |
23724 次 |
| 最近记录: |