Uni-WHAT!?
Unicode errors in python 2.7
import sys
reload(sys)
sys.setdefaultencoding('utf8')
If you see this it means the person who wrote the python code received a unicode error, didn't know how to fix it properly, then Google'd the error and found this quick solution. Here's a good stack overflow post on why not to use setdefaultencoding()
The short answer is, in Python 2 the default encoding is ASCII. When it encounters other encodings it will raise errors. When you change the default encoding you can't be sure whats going out of your code is what came in. Also, when you reload a module, you actually get two copies of the module in your runtime which can cause some weird issues.
So how to deal with different encodings? use decode() and deal with the text as it comes in. Use var.decode(encoding) to decode a byte string as text. Then use var.encode(encoding) to encode a text string as bytes.
Better yet, Python3 treats all strings a sequences of Unicode characters. There is no such thing as a Python string encoded in utf-8. So maybe just use python 3 if you can!
Comments
Post a Comment