App Engine中的Python unicode字符串操作失败

use*_*761 4 google-app-engine character-encoding python-2.7

在开发/本地计算机上运行的Python代码,但在安装到Appengine后失败:

我文件中的第一行:

# -*- coding: utf8 -*-O
Run Code Online (Sandbox Code Playgroud)

代码后面的行:

s1 = u'Ismer?seid'
logging.info (s1)
s2 = s1 + u':' + s1
logging.info (s2)
logging.info ("%s,%s", s1, s2)
Run Code Online (Sandbox Code Playgroud)

在Dev(localhost)中:

INFO     2012-12-18 04:01:17,926 AppRun.py:662] Ismer?seid,
INFO     2012-12-18 04:01:17,926 AppRun.py:664] Ismer?seid:Ismer?seid
INFO     2012-12-18 04:01:17,926 AppRun.py:665] Ismer?seid,Ismer?seid. Ó,
Run Code Online (Sandbox Code Playgroud)

安装/运行后在App Engine上:

I 2012-12-21 06:52:07.730 
É, Á, Ö, Ü. Ó,

E 2012-12-21 06:52:07.736

Traceback (most recent call last):
  File "....", line 672, in xxxx
    s3 = s1 + u':' + s1
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我试过各种编码/解码/等组合.我也在粘贴的字符串'Ismerőseid'上有chardet它给了我 {'confidence': 0.7402600692642154, 'encoding': 'ISO-8859-2'}

任何帮助是极大的赞赏!

vos*_*usa 5

将这3行放在Python 27代码的顶部以使用unicode:

#!/usr/bin/python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

# And this code will not give you any problems

s1 = 'É, Á, Ö, Ü. Ó,'
logging.info (s1)
s2 = s1 + ':' + s1
logging.info ("%s,%s", s1, s2)
Run Code Online (Sandbox Code Playgroud)

永远不要用户str().只有你真的需要!

并阅读尼克约翰逊的这篇博文.这是在Python 27之前.他没有使用from __future__ import unicode_literals,这使得使用Python的unicode如此简单.