I know how to convert string to unicode using unicode("string", "utf-8") and I tried applying it to a dictionary. Well, my first solution is to loop everything (key and value) then convert them to unicode. But is there a fastest way to do this?
dict = {'firstname' : 'Foo', 'lastname' : 'Bar'}
To
dict = {u'firstname' : u'Foo', u'lastname' : u'Bar'}
Any help would be much appreciated. Thanks.
asked Nov 10, 2013 at 5:45
Boy Pasmo
8,59114 gold badges44 silver badges71 bronze badges
-
1There's no shortcut - @perreal's answer is short and easy.Tim Peters– Tim Peters2013年11月10日 05:52:29 +00:00Commented Nov 10, 2013 at 5:52
1 Answer 1
Just use the 'unicode' function:
d = {'firstname' : 'Foo', 'lastname' : 'Bar'}
d = {unicode(k):unicode(v) for k,v in d.items() }
answered Nov 10, 2013 at 5:50
perreal
98.7k23 gold badges159 silver badges187 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Boy Pasmo
Wow! Never thought about this. Thanks!
lang-py