本文介绍了Python瓶请求和unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python瓶构建一个小型的RESTful API,并且在处理请求对象时当前遇到字符编码问题.

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

对于包含表单urlencoded密钥q且值为äöü的POST请求也是如此. request.forms.get('q')包含äöü".

这是怎么回事?我真的没有选择用其他编码对这些元素进行解码的选择吗?瓶子是否可以将其存储为unicode?

谢谢.

解决方案

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

如果打印该字节字符串,并且将其打印的位置将其解释为ISO-8859-1或类似的Windows代码页1252,则将得到äöü.如果要通过打印到Windows命令提示符或记事本显示的文件进行调试,这就是原因.

如果您使用替代直接属性访问 request.query.qforms.q瓶子将而是提供Unicode字符串,使用UTF-8从字节版本解码.通常最好在任何可能的地方使用这些Unicode字符串. (尽管您仍然可能无法将它们打印到控制台.众所周知,Windows命令提示符在处理非ASCII字符时非常糟糕,因此,这是调试Unicode问题的好地方.)

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.)

这篇关于Python瓶请求和unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:28