又遇到 python 2.x 的編碼問題,一樣的程式碼在 Mac OS 上可以執行沒問題,放到 Linux上就出錯。但我又不想使 Dirty Hack 的方式去 reload sys.
下面這段 code:
import sys; print(sys.getdefaultencoding()) print(sys.stdout.encoding)
在 Mac OS 上執行結果:
ascii
UTF-8
在 Linux 上執行結果:
ascii
ANSI_X3.4-1968
在 Linux 上執行截圖:
我的完整 source code:
#!/usr/bin/env python #encoding=utf-8 import sys; print(sys.getdefaultencoding()) print(sys.stdout.encoding) import json data='{"title": "Max Yao\u7684Dropboxlike"}' json_obj = None try : json_obj = json.loads(data) print json_obj['title'] except Exception as err: print "err", err json_obj = None
應急解法:
in the “/etc/environment” file,add line:
PYTHONIOENCODING=utf8
但這段加了,其實沒什麼作用,主要的解法,還是在字串都要記得加 u“xxxx”, 全部改用 unicode, 避免混合使用 str 和 unicode.
下一個 Error:
mysql Error: ‘latin-1’ codec can’t encode character u
解法:
http://stackoverflow.com/questions/3942888/unicodeencodeerror-latin-1-codec-cant-encode-character
I ran into this same issue when using the Python MySQLdb module. Since MySQL will let you store just about any binary data you want in a text field regardless of character set, I found my solution here:
Using UTF8 with Python MySQLdb
Edit: Quote from the above URL to satisfy the request in the first comment…
“UnicodeEncodeError:’latin-1′ codec can’t encode character …”
This is because MySQLdb normally tries to encode everythin to latin-1. This can be fixed by executing the following commands right after you’ve etablished the connection:
db.set_character_set(‘utf8’)
dbc.execute(‘SET NAMES utf8;’)
dbc.execute(‘SET CHARACTER SET utf8;’)
dbc.execute(‘SET character_set_connection=utf8;’)
“db” is the result of MySQLdb.connect(), and “dbc” is the result of db.cursor().
相關文章:
Why should we NOT use sys.setdefaultencoding(“utf-8”) in a py script?
http://stackoverflow.com/questions/3828723/why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script
有關 Python 2 和 Sublime Text 中文 Unicode 編碼問題的分析與理解
https://read01.com/jjkMx.html
有关 Python 2 和 Sublime Text 中文 Unicode 编码问题的分析与理解
http://blog.csdn.net/saghir/article/details/50396660