Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sae环境下在reply返回的数据中有中文时会出现编码错误 #1

Closed
wants to merge 1 commit into from

Conversation

yishh
Copy link

@yishh yishh commented Feb 19, 2013

sae环境下在reply返回的数据中有中文时会出现编码错误,encode成str后可以修正该问题

sae环境下在reply返回的数据中有中文时会出现编码错误,encode成str后可以修正该问题
@whtsky
Copy link
Collaborator

whtsky commented Feb 19, 2013

似乎这样修改会丧失对 Python3 的支持?
https://travis-ci.org/whtsky/WeRoBot/jobs/4894096

Python3 中对字符串进行 encode 之后就会变成 bytes ,而 bytes 是没有 format 的……

比较安全的做法是,在 werobot/utils.py 里面增加一个函数:

def utf8(value):
    if isinstance(value, (bytes, type(None), str)):
        return value
    if isinstance(value, int):
        return str(value)
    assert isinstance(value, unicode)
    return value.encode('utf-8')

然后在这里调用这个函数进行 encode .这样在 Python 3 下不会出问题.

@yishh
Copy link
Author

yishh commented Feb 19, 2013

额,没想过python3的情况。
呵呵,一直还没用过python3.
你的这种方式应该是可以的。

于 13-2-19 下午1:03, whtsky 写道:

似乎这样修改会丧失对 Python3 的支持?
https://travis-ci.org/whtsky/WeRoBot/jobs/4894096

Python3 中对字符串进行 encode 之后就会变成 bytes ,而 bytes 是没有
|format| 的……

比较安全的做法是,在 |werobot/utils.py| 里面增加一个函数:

def utf8(value):
if isinstance(value, (bytes, type(None), str)):
return value
if isinstance(value, int):
return str(value)
assert isinstance(value, unicode)
return value.encode('utf-8')

然后在这里调用这个函数进行 encode .这样在 Python 3 下不会出问题.


Reply to this email directly or view it on GitHub
#1 (comment).

@whtsky
Copy link
Collaborator

whtsky commented Feb 19, 2013

@yishh 方便的话去修改一下代码吧,我好 merge

@whtsky
Copy link
Collaborator

whtsky commented Feb 19, 2013

好吧..

@whtsky whtsky closed this Feb 19, 2013
@whtsky
Copy link
Collaborator

whtsky commented Feb 19, 2013

测试了一下,如果这么修改的话传入 unicode 字符串会报错.

whtsky added a commit that referenced this pull request Feb 19, 2013
@whtsky
Copy link
Collaborator

whtsky commented Feb 19, 2013

@yishh 现在应该正常了

@dalang
Copy link

dalang commented Mar 28, 2013

ArticlesReply 当article.title和article.despcription包含中文时,在sae也会有此错误。
添加上to_unicode 就不会了。
for article in self._articles:
items.append(ArticlesReply.ITEM_TEMPLATE.format(
title=to_unicode(article.title),
description=to_unicode(article.description),
img=article.img,
url=article.url
))

@whtsky
Copy link
Collaborator

whtsky commented Mar 30, 2013

@dalang 修复了。
下次直接发 Pull Request 吧 >_<

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants