【发布时间】:2012-11-22 10:04:43
【问题描述】:
我想将bottle (bottlepy, bottle.py) 用于字典应用程序,允许在 URL (GET) 中指定输入,例如
http://dictionary_domain/lookup/Thanksgiving
使用
@bottle.route('/lookup/<word>')
def request( word="" ):
print(word)
问题是,非 ASCII 字符是 URL-encoded(通常,浏览器会处理这个问题),并且 bottle.py 的解析或我的一般字符编码设置似乎有问题。 例子:
..lookup/Olivenöl:
"Olivenöl"
..lookup/Öl:
Traceback (most recent call last):
File "bottle.py", line 763, in _handle
return route.call(**args)
File "bottle.py", line 1572, in wrapper
rv = callback(*a, **ka)
File "dictionary.py", line 63, in request
print( "bottle: \"{}\" requested".format( word ) )
File "C:\Python32\lib\encodings\cp850.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\x96' in position 10: character maps to <undefined>
有问题的字符编码为
capital Ö -> %C3%96
minscule ö -> %C3%B6
输出解码为 ö -> c3b6
但我不知道如何修复编码混乱.. 我想bottle.py 需要处理这个问题。
脚本使用# -*- coding: utf-8 -*-(文件也是带有BOM的UTF-8),常规字符串打印到控制台就好了。
软件版本: 瓶子.py:“0.11.4”和“0.12-dev” Python 3.2.3(默认,2012 年 4 月 11 日,07:15:24)[MSC v.1500 32 位(英特尔)] 在 win32 上
【问题讨论】:
-
我已经在上述群组中发布了这个问题:groups.google.com/forum/?fromgroups=#!forum/bottlepy(线程尚未显示)
-
请参阅上面的帖子以获取来自 bottle.py 的作者 Marcel Hellkamp 的解释和解决方案。
-
非常感谢您提供链接!
标签: python url encoding bottle