中文格式python 打印json格式的数据中文显示问题
2013-06-24 20:35
736 查看
废话就不多说了,开始。。。
平日会有这么一个应用场景,会用python去处置json格式的web API,以“ip.taobao.com”的API为例,详情见http://ip.taobao.com/instructions.php
是一个查询ip地址归属地的接口,其中包含国度、地域和ISP这些信息,均为中文表现。
我会用如下方式获取API数据:
如上函数使用了urllib和json这2个模块,其中urllib用于请求页面获取json格式的数据;而json模块用于返回json格式数据;但是当你使用print来输出”myjson“这个变量的时候会发现中文全是unicode表现,可以用:
让其表现成中文,默许是以ASCII来解析code的,中文不在ASCII编码当中,自然没法表现,可以看到json.dumps的帮助信息也说明白设为False后以:
每日一道理
俄国作家契诃夫说:“有大狗,有小狗,小狗不该因为大狗的存在而心慌意乱。所有的狗都应该叫,就让他各自用上帝给他的声音。
最后附图一张,可以看到表现中文正常了:
![](http://img.blog.csdn.net/20130624001850046?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb253ZXIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
文章结束给大家分享下程序员的一些笑话语录:
开发时间
项目经理: 如果我再给你一个人,那可以什么时候可以完工?程序员: 3个月吧!项目经理: 那给两个呢?程序员: 1个月吧!
项目经理: 那100呢?程序员: 1年吧!
项目经理: 那10000呢?程序员: 那我将永远无法完成任务.
---------------------------------
原创文章 By
中文和格式
---------------------------------
平日会有这么一个应用场景,会用python去处置json格式的web API,以“ip.taobao.com”的API为例,详情见http://ip.taobao.com/instructions.php
是一个查询ip地址归属地的接口,其中包含国度、地域和ISP这些信息,均为中文表现。
我会用如下方式获取API数据:
myjson = json.loads(urllib.urlopen(url).read())
如上函数使用了urllib和json这2个模块,其中urllib用于请求页面获取json格式的数据;而json模块用于返回json格式数据;但是当你使用print来输出”myjson“这个变量的时候会发现中文全是unicode表现,可以用:
newjson = json.dumps(myjson, ensure_ascii=False)
让其表现成中文,默许是以ASCII来解析code的,中文不在ASCII编码当中,自然没法表现,可以看到json.dumps的帮助信息也说明白设为False后以:
每日一道理
俄国作家契诃夫说:“有大狗,有小狗,小狗不该因为大狗的存在而心慌意乱。所有的狗都应该叫,就让他各自用上帝给他的声音。
>>> help(json.dumps) Help on function dumps in module json: dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=Tr e, cls=None, indent=None, separators=None, encoding='utf-8', default=None, **kw Serialize ``obj`` to a JSON formatted ``str``. If ``skipkeys`` is ``True`` then ``dict`` keys that are not basic types (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) will be skipped instead of raising a ``TypeError``. If ``ensure_ascii`` is ``False``, then the return value will be a ``unicode`` instance subject to normal Python ``str`` to ``unicode`` coercion rules instead of being escaped to an ASCII ``str``. If ``check_circular`` is ``False``, then the circular reference check for container types will be skipped and a circular reference will result in an ``OverflowError`` (or worse). If ``allow_nan`` is ``False``, then it will be a ``ValueError`` to serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in strict compliance of the JSON specification, instead of using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). If ``indent`` is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. ``None`` is the most compact representation. If ``separators`` is an ``(item_separator, dict_separator)`` tuple then it will be used instead of the default ``(', ', ': ')`` separators. ``(',', ':')`` is the most compact JSON representation. ``encoding`` is the character encoding for str instances, default is UTF-8. ``default(obj)`` is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with the ``cls`` kwarg. >>>
最后附图一张,可以看到表现中文正常了:
文章结束给大家分享下程序员的一些笑话语录:
开发时间
项目经理: 如果我再给你一个人,那可以什么时候可以完工?程序员: 3个月吧!项目经理: 那给两个呢?程序员: 1个月吧!
项目经理: 那100呢?程序员: 1年吧!
项目经理: 那10000呢?程序员: 那我将永远无法完成任务.
---------------------------------
原创文章 By
中文和格式
---------------------------------
相关文章推荐
- python 打印json格式的数据中文显示问题
- python 打印json格式的数据中文显示问题
- json数据的中文显示问题 python
- python中json数据中文编码显示的问题
- 【转载】json数据的中文显示问题 python
- Python json数据中文输出问题。
- Python下调用json.dumps中文显示问题解决办法
- JSON: json格式的中文显示问题解决方法
- Python实现的json文件读取及中文乱码显示问题解决方法
- 【已解决】为什么Python打印数据显示为b‘XXX’格式
- ajax根据ID查询数据库并返回Json格式数据返回js,使用append显示到页面。判断json值为[]或者[[]]的问题。
- python中文编码问题深入分析(二):print打印中文异常及显示乱码问题分析与解决
- php返回json数据中文显示的问题
- php返回json数据中文显示的问题
- 解决python3 json数据包含中文的读写问题
- springMVC_json格式数据的接收和发送(未解决中文乱码问题)
- springMVC_json格式数据的接收和发送(未解决中文乱码问题)
- php返回json数据中文显示的问题
- [json] -- php返回json数据中文显示的问题
- 通过python直接显示json格式数据的方法