App Engine中的Python unicode字符串操作失败 [英] Python unicode string operation in App Engine failing
问题描述
在开发/本地机器中运行但在安装到Appengine后失败的Python代码:
我的文件中的第一行:
# - * - coding:utf8 - * - O
后面的代码行:
s1 =u'Ismerőseid'
logging.info )
s2 = s1 + u':'+ s1
logging.info(s2)
logging.info(%s,%s,s1,s2)
在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。 Ó,
在App Engine上安装/运行后:
$ b bI 2012-12-21 06:52:07.730
É,Á,Ö,Ü。 Ó,
E 2012-12-21 06:52:07.736
回溯(最近最后一次调用):
文件....,行672,in xxxx
s3 = s1 + u':'+ s1
UnicodeDecodeError:'ascii'编解码器无法解码位置5中的字节0xc3:序数不在范围内(128)
我已经尝试过各种编码/解码等组合。我也有chardet上粘贴的字符串'Ismerőseid'它给我
{'confidence':0.7402600692642154,'encoding':'ISO-8859-2'}
非常感谢任何帮助。
解决方案将这3行放在Python 27代码的顶部使用unicode:
#!/ usr / bin / python
# - * - coding:utf-8 - * -
__future__ import unicode_literals
#这个代码不会给你任何问题
s1 ='É,Á,Ö,Ü。 Ó,'
logging.info(s1)
s2 = s1 +':'+ s1
logging.info(%s,%s,s1,s2)
从不使用str()。只有当你真的需要!
并阅读这篇博客。这是在Python 27之前。他没有使用来自__future__ import unicode_literals 的
,这使得使用unicode和Python很容易。
Python code that runs in Development/local machine, but fails after installing to Appengine :
1st line in my File :
# -*- coding: utf8 -*-O
Lines later in the code :
s1 = u'Ismerőseid' logging.info (s1) s2 = s1 + u':' + s1 logging.info (s2) logging.info ("%s,%s", s1, s2)
In 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. Ó,
On App Engine after install/run :
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)
I have tried to various combination of encoding/decoding/etc.. I have also chardet on the pasted string 'Ismerőseid' and it gives me
{'confidence': 0.7402600692642154, 'encoding': 'ISO-8859-2'}
Any help is greatly appreciated!
解决方案Put these 3 lines on the top of your Python 27 code to use 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)
And never user str(). Only if you realy need to!
And read this blogpost from Nick Johnson. This was before Python 27. He did not use the
from __future__ import unicode_literals
, which makes using unicode with Python so easy.这篇关于App Engine中的Python unicode字符串操作失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!