Google App Engine与Python translate()兼容吗?

2024-04-19 05:57:58 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面,我使用translate()来消除字符串中的标点符号。我在translate上遇到了很多问题,因为它不适用于unicode。但是现在我注意到这个脚本在开发服务器上运行得很好,但是在生产服务器上却出现了一个错误。在

请求通过chrome扩展发送到googleappengine。生产服务器中的任何建议我都可以修复?或者有没有另一种不使用translate()来消除标点符号的方法呢。在

生产服务器中的日志:

2011-10-11 06:18:10.384
get_rid_of_unicode: ajax: how to use xmlhttprequest
E 2011-10-11 06:18:10.384
expected a character buffer object
Traceback (most recent call last):
  File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/_webapp25.py", line 703, in __call__
    handler.post(*groups)
  File "/base/data/home/apps/ting-1/1.353888928453510037/ting.py", line 2073, in post
    user_tag_list_case = f1.striplist(main().split(" "))
  File "/base/data/home/apps/ting-1/1.353888928453510037/ting.py", line 2055, in main
    title_no_punctuation = get_rid_of_unicode.translate(None, string.punctuation)
TypeError: expected a character buffer object

相同的脚本在开发服务器中正常工作:

^{pr2}$

剧本:

def main():

    title_lowercase = title.lower()
    title_without_possessives = remove_possessive(title_lowercase)
    title_without_double_quotes = remove_double_quotes(title_without_possessives)
    get_rid_of_unicode = title_without_double_quotes.encode('utf-8')
    title_no_punctuation = get_rid_of_unicode.translate(None, string.punctuation)
    back_to_unicode = unicode(title_no_punctuation, "utf-8")
    clean_title = remove_stop_words(back_to_unicode, f1.stop_words)
    return clean_title

user_tag_list = []
user_tag_list_case = f1.striplist(main().split(" "))
for tag in user_tag_list_case:
    user_tag_list.append(tag.lower())

Tags: ofin服务器gettitlemaintagunicode
1条回答
网友
1楼 · 发布于 2024-04-19 05:57:58

googleappengine运行python2.5.2。str.translate()需要256个字符的字符串作为第一个参数;None自python2.6以来一直是允许的值。在

相关问题 更多 >