Boy Pasmo
Boy Pasmo

Reputation: 8511

convert dictionary key and value to unicode

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.

Upvotes: 0

Views: 753

Answers (1)

perreal
perreal

Reputation: 98118

Just use the 'unicode' function:

d = {'firstname' : 'Foo', 'lastname' : 'Bar'}
d = {unicode(k):unicode(v) for k,v in d.items() }

Upvotes: 3

Related Questions