Python 将每个字典值转换为 utf-8(字典理解?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33699343/
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
Convert every dictionary value to utf-8 (dictionary comprehension?)
提问by PurpleVermont
I have a dictionary and I want to convert every value to utf-8. This works, but is there a "more pythonic" way?
我有一本字典,我想将每个值都转换为 utf-8。这有效,但有没有“更pythonic”的方式?
for key in row.keys():
row[key] = unicode(row[key]).encode("utf-8")
For a list I could do
对于我可以做的清单
[unicode(s).encode("utf-8") for s in row]
but I'm not sure how to do the equivalent thing for dictionaries.
但我不确定如何为字典做同样的事情。
This is different from Python Dictionary Comprehensionbecause I'm not trying to create a dictionary from scratch, but from an existing dictionary. The solutions to the linked question do not show me how to loop through the key/value pairs in the existing dictionary in order to modify them into new k/v pairs for the new dictionary. The answer (already accepted) below shows how to do that and is much clearer to read/understand for someone who has a task similar to mine than the answers to the linked related question, which is more complex.
这与Python Dictionary Comprehension不同,因为我不是试图从头开始创建字典,而是从现有字典中创建字典。链接问题的解决方案没有向我展示如何遍历现有字典中的键/值对,以便将它们修改为新字典的新 k/v 对。下面的答案(已被接受)显示了如何做到这一点,对于与我的任务类似的人来说,阅读/理解比链接相关问题的答案更清晰,后者更复杂。
采纳答案by That1Guy
Use a dictionary comprehension. It looks like you're starting with a dictionary so:
使用字典理解。看起来你是从字典开始的,所以:
mydict = {k: unicode(v).encode("utf-8") for k,v in mydict.iteritems()}
The example for dictionary comprehensions is near the end of the block in the link.
字典理解的示例位于链接中块的末尾附近。
回答by ergonaut
You can just iterate through the keys if you wanted to:
如果你想,你可以遍历键:
{x:unicode(a[x]).encode("utf-8") for x in a.keys()}
回答by Alastair McCormack
It depends why you're implicitly encoding to UTF-8. If it's because you're writing to a file, the pythonic way is to leave your strings as Unicode and encode on output:
这取决于您隐式编码为 UTF-8 的原因。如果是因为您正在写入文件,那么 Pythonic 的方法是将您的字符串保留为 Unicode 并在输出时进行编码:
with io.open("myfile.txt", "w", encoding="UTF-8") as my_file:
for (key, values) in row.items():
my_string = u"{key}: {value}".format(key=key, value=value)
my_file.write(my_string)
回答by Anurag Misra
Best approach to convert non-asciidictionary value in asciicharacters is
在ascii字符中转换非 ascii字典值的最佳方法是
mydict = {k: unicode(v, errors='ignore').encode('ascii','ignore') for k,v in mydict.iteritems()}
Best approach to convert non-utf-8dictionary value in utf-8characters is
在utf-8字符中转换非 utf-8字典值的最佳方法是
mydict = {k: unicode(v, errors='ignore').encode('utf-8','ignore') for k,v in mydict.iteritems()}
For more reference read python unicode documentation
有关更多参考,请阅读python unicode 文档
回答by Henri Chabert
As I had this problem as well, I built a very simple function that allows any dict to be decoded in utf-8 (The problem with the current answer is that it applies only for simple dict).
由于我也有这个问题,我构建了一个非常简单的函数,它允许以 utf-8 对任何 dict 进行解码(当前答案的问题是它仅适用于简单的 dict)。
If it can help anyone, it is great, here is the function :
如果它可以帮助任何人,那就太好了,这是功能:
def utfy_dict(dic):
if isinstance(dic,unicode):
return(dic.encode("utf-8"))
elif isinstance(dic,dict):
for key in dic:
dic[key] = utfy_dict(dic[key])
return(dic)
elif isinstance(dic,list):
new_l = []
for e in dic:
new_l.append(utfy_dict(e))
return(new_l)
else:
return(dic)
回答by kjmerf
Python 3 version building on that one answer by That1Guy.
Python 3 版本建立在 That1Guy 的那个答案之上。
{k: str(v).encode("utf-8") for k,v in mydict.items()}