0

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
1
  • 1
    There's no shortcut - @perreal's answer is short and easy. Commented Nov 10, 2013 at 5:52

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Wow! Never thought about this. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.