get_argument返回unicode数据类型的问题
2015-02-02 21:51
176 查看
Chapter: Tornado表单处理
1. 使用get_argument获取url
query参数
2. 为何get_argument不区分POST与GET?
3. get_argument返回unicode数据类型的问题
在 Tornado 里,self.get_argument返回的数据类型为unicode:
get_argument在获取数据的时候,会进行decode("utf-8")操作,因为get_argument最终调用了tornado.escape下面的to_unicode方法,也就是argument会通过decode("utf-8")来转成unicode:
get_argument获取数据之后一般需要先使用u.encode('utf-8')转换成string类型后才能使用。
如果用get_argument无法获取数据,可以用更加原始的方法通过self.request.arguments获取GET或者POST的所有参数字典,这个字典是未经过decode处理的原生参数,每个参数都是字典里面的一项,主要每个参数对应的项都是一个列表。
1. 使用get_argument获取url
query参数
2. 为何get_argument不区分POST与GET?
3. get_argument返回unicode数据类型的问题
在 Tornado 里,self.get_argument返回的数据类型为unicode:
1 | chars = self .get_argument( 'chars' ) |
2 | self .write( str ( type (chars)) ) |
3 | # 输出 |
4 | # <type 'unicode'> |
01 | def to_unicode(value): |
02 | """Converts a string argument to a unicode string. |
03 |
04 | If the argument is already a unicode string or None, it is returned |
05 | unchanged. Otherwise it must be a byte string and is decoded as utf8. |
06 | """ |
07 | if isinstance (value, _TO_UNICODE_TYPES): |
08 | return value |
09 | assert isinstance (value, bytes) |
10 | return value.decode( "utf-8" ) |
11 |
12 | # to_unicodewas previously named _unicodenot because it was private, |
13 | # but to avoid conflicts with the built-in unicode() function/type |
14 | _unicode = to_unicode |
如果用get_argument无法获取数据,可以用更加原始的方法通过self.request.arguments获取GET或者POST的所有参数字典,这个字典是未经过decode处理的原生参数,每个参数都是字典里面的一项,主要每个参数对应的项都是一个列表。
相关文章推荐
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
- 解决问题:SSIS中,列“column”无法在 unicode和非unicode字符串数据类型之间转换
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式问题的方法
- python 输出JSON类型数据时遇到的编码问题(utf8,unicode)
- $.getjson遇到的几个问题-json返回数据中带有html标签的输出
- Xutils的get请求后,总是返回同样数据的问题解决方式
- Mybatis 查询int类型数据,返回记录条数为0时报错问题解决
- Redis之数据Set和Get类型不一致问题
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
- servlet返回application/json数据类型在ie浏览器中提示下载文件问题
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
- asp.net 使用ajax请求地址接口数据 ajax返回类型是json js get post请求数据
- $.getJSON()处理数据及IE返回错误的问题
- Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
- dhl:ajax无法跨域改用getJSON(解决服务器返回json数据中文为乱码的问题$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; chars)
- ajax实现返回数据是html类型的跨域问题
- Spring学习笔记(三十四):关于http的get请求返回的数据过长问题
- 解决Asp.net Mvc返回JsonResult中DateTime类型数据格式问题的方法
- 用JS解决Asp.net Mvc返回JsonResult中DateTime类型数据格式的问题
- Xutils 使用get请求时,连续请求返回数据相同问题的解决