Skip to content

Instantly share code, notes, and snippets.

@deehzee
Last active February 8, 2017 13:13
Show Gist options
  • Save deehzee/7baa7dfcbb0f749bf6f8153aab6d664f to your computer and use it in GitHub Desktop.
Save deehzee/7baa7dfcbb0f749bf6f8153aab6d664f to your computer and use it in GitHub Desktop.
Setting Default Encoding to Unicode in Python
# 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