Python的瓶子请求和unicode[英] Python bottle requests and unicode

本文是小编为大家收集整理的关于Python的瓶子请求和unicode的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在使用python建造一个带有瓶子的小静态API,目前正在使用请求对象时遇到字符编码的问题.

击中http://server.com/api?q=äöü并在服务器上查看request.query['q']让我"€€",这显然不是我想要的.

对于包含带有form-urlencoded键q的帖子请求,值äöü也是如此.在

这里发生了什么?我真的没有选择用不同的编码来解码这些元素还是我?瓶子是否可以将它们存储在Unicode中?

是否有一般选择?

谢谢.

推荐答案

request.query['q']和forms.get('q')返回Web浏览器提交的原始字节值.由浏览器以UTF-8编码字节提交的值äöü是'\xc3\xa4\xc3\xb6\xc3\xbc'.

如果您打印该字节字符串,并且您要打印的位置将其解释为ISO-8859-1或类似的Windows Code Page Page 1252,则您将获得äöü.如果要通过打印到Windows命令提示符或Notepad显示的文件来调试,这就是原因.

如果您使用替代直接属性访问 request.query.q或forms.q代替使用UTF-8从字节版本解码的Unicode字符串.通常最好在任何地方使用这些Unicode字符串. (尽管您仍然很难将它们打印到控制台上.众所周知,Windows命令提示在应对非ASCII字符方面非常糟糕,因此是一个糟糕的地方,不得不调试Unicode问题.)

其他推荐答案

也可以选择def getunicode(self, name, default=None, encoding=None)

而不是使用

request.forms.get('some_form_field_name')

尝试

request.forms.getunicode('some_form_field_name')

将处理非拉丁字符.

其他推荐答案

在这种情况下,要转换它,我确实喜欢此search_field.encode(" iso-8859-1").解码(" utf-8")

本文地址:https://www.itbaoku.cn/post/1975350.html

问题描述

I'm building a small RESTful API with bottle in python and am currently experiencing an issue with character encodings when working with the request object.

Hitting up http://server.com/api?q=äöü and looking at request.query['q'] on the server gets me "äöü", which is obviously not what I'm looking for.

Same goes for a POST request containing the form-urlencoded key q with the value äöü. request.forms.get('q') contains "äöü".

What's going on here? I don't really have the option of decoding these elements with a different encoding or do I? Is there a general option for bottle to store these in unicode?

Thanks.

推荐答案

request.query['q'] and forms.get('q') return the raw byte value submitted by the web browser. The value äöü, submitted by a browser as UTF-8 encoded bytes, is '\xc3\xa4\xc3\xb6\xc3\xbc'.

If you print that byte string, and the place you're printing it to interprets it as ISO-8859-1, or the similar Windows code page 1252, you will get äöü. If you are debugging by printing to a Windows command prompt, or a file displayed by Notepad, that's why.

If you use the alternative direct property access request.query.q or forms.q Bottle will give you Unicode strings instead, decoded from the byte version using UTF-8. It's usually best to work with these Unicode strings wherever you can. (Though still you may have trouble printing them to console. The Windows command prompt is notoriously terrible at coping with non-ASCII characters, and as such is a bad place to be debugging Unicode issues.)

其他推荐答案

There's also the alternative of using def getunicode(self, name, default=None, encoding=None)

Instead of using

request.forms.get('some_form_field_name')

try

request.forms.getunicode('some_form_field_name')

That'll handle non-latin characters.

其他推荐答案

in this case, to convert it ,I did like this search_field.encode("ISO-8859-1").decode("utf-8")