Skip to content

Instantly share code, notes, and snippets.

@zhenyi2697
Created February 24, 2013 10:32
Show Gist options
  • Save zhenyi2697/5023355 to your computer and use it in GitHub Desktop.
Save zhenyi2697/5023355 to your computer and use it in GitHub Desktop.
Python: unicode conversion
#!/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