Python 将列表中的字典键从 unicode 编码为 ascii

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4303324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:10:26  来源:igfitidea点击:

Encode keys of dictionaries inside a list from unicode to ascii

pythondjangounicodeencodingdictionary

提问by mastodon

I have sample response with friends list from facebook:

我有来自 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'}]

How I could parse through this list encoding key's of the dictionaries to ascii ? I've tried something like this :

我如何将字典的这个列表编码键解析为 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

But encoded keys are not saved and as a result I'm still getting unicode values.

但是编码的键没有保存,因此我仍然得到 unicode 值。

采纳答案by Karl Knechtel

First: do you really needto do this? The strings are in Unicode for a reason: you simply can't represent everything in plain ASCII that you can in Unicode. This probably won't be a problem for your dictionary keys 'uid', 'name' and 'pic_small'; but it probably won't be a problem to leave them as Unicode, either. (The 'simplejson' library does not know anything about your data, so it uses Unicode for every string - better safe than sorry.)

第一:你真的要这样做吗?字符串采用 Unicode 是有原因的:您根本无法用纯 ASCII 表示所有内容,而您可以用 Unicode 表示。这对于您的字典键 'uid'、'name' 和 'pic_small' 来说可能不是问题;但将它们保留为 Unicode 也可能不是问题。('simplejson' 库对您的数据一无所知,因此它对每个字符串使用 Unicode - 比抱歉更安全。)

Anyway:

反正:

In Python, strings cannot be modified. The .encodemethod does not change the string; it returns a new string that is the encoded version.

在 Python 中,字符串不能被修改。该.encode方法不改变字符串;它返回一个新的字符串,它是编码版本。

What you want to do is produce a new dictionary, which replaces the keys with the encoded keys. We can do this by passing each pair of (encoded key, original value) as *args for the dict constructor.

您想要做的是生成一个新字典,用编码的键替换键。我们可以通过将每对(编码键,原始值)作为 *args 传递给 dict 构造函数来实现。

That looks like:

看起来像:

dict((k.encode('ascii'), v) for (k, v) in original.items())

Similarly, we can use a list comprehension to apply this to every dictionary, and create the new list. (We canmodify the list in-place, but this way is cleaner.)

类似地,我们可以使用列表理解将其应用于每个字典,并创建新列表。(我们可以就地修改列表,但这种方式更简洁。)

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

回答by Russell Borogove

Your other responses hint at this but don't come out and say it: dictionary lookup and string comparison in Python transparently convert between Unicode and ASCII:

您的其他回复暗示了这一点,但不要出来说出来: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

So for most uses of a dictionary converted from JSON, you don't usually need to worry about the fact that everything's Unicode.

因此,对于从 JSON 转换而来的字典的大多数用途,您通常不需要担心一切都是 Unicode 的事实。