Last active
February 8, 2017 13:13
-
-
Save deehzee/7baa7dfcbb0f749bf6f8153aab6d664f to your computer and use it in GitHub Desktop.
Setting Default Encoding to Unicode in Python
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
# reset-sys-unicode.py | |
# | |
# http://stackoverflow.com/questions/21129020/how-to-fix-unicodedecodeerror-ascii-codec-cant-decode-byte | |
# | |
# to encode a text with utf8 in python2 | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf8') | |
text = file('data/xxx.txt').read() | |
type(text) # -> 'str' | |
utxt = unicode(text) | |
type(utxt) #-> 'unicode' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment