linux : 各个发行版中修改python27默认编码为utf-8

该方法可解决robot报错:'ascii' codec can't encode character u'\xf1' in position 16: ordinal not in  range(128)

 

在下面目录中新增文件:sitecustomize.py

内容为

#coding=utf-8
import sys

reload(sys)
sys.setdefaultencoding('utf8')

各个发行版放置位置:

 

RedHat:

/usr/local/python27/lib/python2.7/site-packages/

 

Ubuntu

/usr/lib/python2.7

 

其他发行版

/usr/local/lib/python.27/site-packages

 

测试一下:使用sys.getdefaultencoding()测试,看返回是不是utf8

[root@base7565 site-packages]# python
Python 2.7.14 (default, Sep  2 2019, 14:26:39)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'utf8'
>>>

 

当robot通过cx_Oracle连接Oracle数据库时如果sql语句中出现中文,需要将文件内容修改为:

#coding=utf-8
import sys


reload(sys)
sys.setdefaultencoding('utf8')

import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

 

posted @ 2019-09-03 09:22  秋寻草  阅读(1129)  评论(0编辑  收藏  举报