Created
October 11, 2017 12:26
-
-
Save walterrenner/0a96ee55125b5070807385880cf5d9f0 to your computer and use it in GitHub Desktop.
Dealing with unicode and strings in Python
This file contains hidden or 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
# Deal exclusively with unicode objects as much as possible | |
# by decoding things to unicode objects when you first get them and | |
# encoding them as necessary on the way out. | |
# https://stackoverflow.com/a/6048203 | |
>>> s = 'abc' | |
>>> type(s) | |
<type 'str'> | |
>>> u = u'abc' # note the u prefix | |
>>> type(u) | |
<type 'unicode'> | |
# convert unicode to string by encoding | |
>>> s = u.encode('utf8') | |
>>> type(s) | |
<type 'str'> | |
# convert string to unicode by decoding | |
>>> u1 = s.decode('utf-8') | |
>>> type(u1) | |
<type 'unicode'> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment