python 打印json格式的数据中文显示问题
来源:互联网 发布:mac ppt 设置动画顺序 编辑:程序博客网 时间:2024/04/29 15:14
通常会有这么一个应用场景,会用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=Tre, 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.>>>
最后附图一张,可以看到显示中文正常了:
- python 打印json格式的数据中文显示问题
- python 打印json格式的数据中文显示问题
- json数据的中文显示问题 python
- python中json数据中文编码显示的问题
- 【转载】json数据的中文显示问题 python
- [json] -- php返回json数据中文显示的问题
- 通过python直接显示json格式数据的方法
- php返回json数据中文显示的问题
- php返回json数据中文显示的问题
- 解决json接口数据中文显示为null的问题
- php返回json数据中文显示的问题
- php返回json数据中文显示的问题
- iOS开发之LLDB打印json数据显示中文
- Spring MVC rest接收json中文格式数据显示乱码
- json格式的时间显示问题
- Python打印中文问题
- 节点数据打印的格式控制问题
- json中文显示乱码的问题
- 最大堆的构造
- NYOJ - 最长回文子串
- oracle反向索引
- 学习路上的点点滴滴
- OpenStack建立实例完整过程源码详细分析(5)
- python 打印json格式的数据中文显示问题
- Cocos2dxActivity cannot be resolved to a type解决方案
- oracle自动分区
- 程序报错 undefined reference to `shm_open'
- Could not find *.apk!解决办法
- VS2008 C++ 项目怎样添加“依赖”、“库目录”和“包含目录”
- CCNA学习笔记1-OSI参考模型
- CCNA学习笔记2-TCP/IP协议栈
- 蚁群算法