环境之间的 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.

乌班图:

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 模块)生成的字典进行操作而收到此错误,但我认为故障不存在。

稍加检查就会发现,这种情况正在外语中发生。

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

"服务器"环境中的默认编码与 Ubuntu 环境不同。 尝试 u['name'].encode('utf-8')

最新更新