Created
February 24, 2013 10:32
-
-
Save zhenyi2697/5023355 to your computer and use it in GitHub Desktop.
Python: unicode conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# To convert a Unicode string into an 8-bit string using a specific encoding, Unicode objects provide an encode() method that takes one argument, the name of the encoding. Lowercase names for encodings are preferred. | |
u"äöü".encode('utf-8') | |
# '\xc3\xa4\xc3\xb6\xc3\xbc' | |
# If you have data in a specific encoding and want to produce a corresponding Unicode string from it, you can use the unicode() function with the encoding name as the second argument. | |
unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8') | |
# u'\xe4\xf6\xfc' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment