HappyPy
HappyPy

Reputation: 10697

convert strings in separate lists to unicode - python

What's the best way to convert every string in a list (containing other lists) to unicode in python?

For example:

[['a','b'], ['c','d']]

to

[[u'a', u'b'], [u'c', u'd']]

Upvotes: 2

Views: 130

Answers (3)

Rohit Jain
Rohit Jain

Reputation: 213261

>>> li = [['a','b'], ['c','d']]

>>> [[v.decode("UTF-8") for v in elem] for elem in li]
[[u'a', u'b'], [u'c', u'd']]

Upvotes: 3

alecxe
alecxe

Reputation: 473873

>>> l = [['a','b'], ['c','d']]
>>> map(lambda x: map(unicode, x), l)
[[u'a', u'b'], [u'c', u'd']]

Upvotes: 0

Homer6
Homer6

Reputation: 15159

Unfortunately, there isn't an easy answer with unicode. But fortunately, once you understand it, it'll carry with you to other programming languages.

This is, by far, the best resource that I've seen for python unicode:

http://nedbatchelder.com/text/unipain/unipain.html

Use the arrow keys (on your keyboard) to navigate to the next and previous slides.

Also, please take a look at this (and the other links from the end of that slideshow).

http://www.joelonsoftware.com/articles/Unicode.html

Upvotes: 0

Related Questions