Convert Every Dictionary Value To Utf-8 (dictionary Comprehension?)
I have a dictionary and I want to convert every value to utf-8. This works, but is there a 'more pythonic' way? for key in row.keys(): row[key] = unico
Solution 1:
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.
Solution 2:
Python 3 version building on that one answer by That1Guy.
{k: str(v).encode("utf-8") for k,v in mydict.items()}
Solution 3:
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).
If it can help anyone, it is great, here is the function :
defutfy_dict(dic):
ifisinstance(dic,unicode):
return(dic.encode("utf-8"))
elifisinstance(dic,dict):
for key in dic:
dic[key] = utfy_dict(dic[key])
return(dic)
elifisinstance(dic,list):
new_l = []
for e in dic:
new_l.append(utfy_dict(e))
return(new_l)
else:
return(dic)
Solution 4:
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:
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)
Solution 5:
You can just iterate through the keys if you wanted to:
{x:unicode(a[x]).encode("utf-8") for x in a.keys()}
Post a Comment for "Convert Every Dictionary Value To Utf-8 (dictionary Comprehension?)"