记录新问题
Python2中遇到UnicodeEncodeError: 'ascii' codec can't encode characters in position 16-20: ordinal not in range(128)
一、网上的解决办法----实测--没用
更改编码方式
二、有效的解决办法
原理不明白,但是有效
在当前使用的python版本的安装目录下的Lib目录,找到site.py,修改def setencoding()方法
如我的是python2.7,路径:/usr/lib/python2.7/,打开site.py找到如下代码更改,if条件,保存即可。
def setencoding():
"""Set the string encoding used by the Unicode implementation. The
default is 'ascii', but if you're willing to experiment, you can
change this."""
encoding = "ascii" # Default value set by _PyUnicode_Init()
if 1:
# Enable to support locale aware default string encodings.
import locale
loc = locale.getdefaultlocale()
if loc[1]:
encoding = loc[1]
if 0:
# Enable to switch off string to Unicode coercion and implicit
# Unicode to string conversion.
encoding = "undefined"
if encoding != "ascii":
# On Non-Unicode builds this will raise an AttributeError...
sys.setdefaultencoding(encoding) # Needs Python Unicode build !