环境之间的Python字符编码不一致

时间:2014-03-05 02:26:30

标签: python encoding character rhel

服务器错误: python 2.7

Traceback (most recent call last):
  File "promoter.py", line 325, in process_unfollowbacks
    self.add_user_to_database(user_info)
  File "promoter.py", line 618, in add_user_to_database
    str(u['name']),
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe7' in position 11: ordinal not in range(128)

然而,它在Ubuntu上运行很好。

服务器:

Python 2.7.3 (default, Mar  2 2014, 10:09:03) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.

Ubuntu的:

Python 2.7.5+ (default, Feb 27 2014, 19:37:08) 
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

我通过操作Twitter Json数组(Twython模块)产生的字典来获取此错误但是我不认为错误存在。

一点点检查表明这种情况正在发生在外语中。

问题: 如何处理环境之间的这种不一致?

1 个答案:

答案 0 :(得分:0)

“服务器”环境中的默认编码与Ubuntu环境不同。试试你的['name']。encode('utf-8')