废话就不多说了,开始。。。

平日会有这么一个应用场景,会用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格式的数据中文显示问题的更多相关文章

  1. python打印表格式数据,留出正确的空格和段落星号或注释

    python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...

  2. python中json格式数据输出实现方式

    python中json格式数据输出实现方式 主要使用json模块,直接导入import json即可. 小例子如下: #coding=UTF-8 import json info={} info[&q ...

  3. python打印表格式数据-星号或注释

    python打印表格式数据,留出正确的空格,格式化打出 代码如下: def printPicnic(itemsDict,leftWidth,rightWidth): print('PICNIC ITE ...

  4. 09: xmltodict 模块将xml格式转成json格式

    1.1 : xmltodict 模块将xml格式转成json格式 <?xml version="1.0"?> <!--#版本号--> <data> ...

  5. Python处理json格式的数据文件(一些坑、一些疑惑)

    这里主要说最近遇到的一个问题,不过目前只是换了一种思路先解决了,脑子里仍然有疑惑,只能怪自己太菜. 最近要把以前爬的数据用一下了,先简单的过滤一下,以前用scrapy存数据的时候为了省事也为了用一下它 ...

  6. python将json格式的数据转换成文本格式的数据或sql文件

    python如何将json格式的数据快速的转化成指定格式的数据呢?或者转换成sql文件? 下面的例子是将json格式的数据准换成以#_#分割的文本数据,也可用于生成sql文件. [root@bogon ...

  7. Python将JSON格式数据转换为SQL语句以便导入MySQL数据库

    前文中我们把网络爬虫爬取的数据保存为JSON格式,但为了能够更方便地处理数据.我们希望把这些数据导入到MySQL数据库中.phpMyadmin能够把MySQL数据库中的数据导出为JSON格式文件,但却 ...

  8. python 将json格式的数据写入csv格式的文件中

    # coding=utf-8 import json import csv # 重新进行配置读写数据时的默认编码 import sys reload(sys) sys.setdefaultencodi ...

  9. python 使用json格式转换

    什么是json: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript Programm ...

随机推荐

  1. javascript3

    计算阶乘函数:<script> function factorial(n){ var product=1; while (n>1){ product*=n;//product=pro ...

  2. Django查询的琐碎记录

    我的需求是这样的,获取指定用户的获“赞”总数. 用户 models.py class UserProfile(models.Model): user = models.OneToOneField(Us ...

  3. 快速构建Windows 8风格应用11-语义缩放

    原文:快速构建Windows 8风格应用11-语义缩放 本篇博文主要介绍为什么需要语义缩放.什么是语义缩放.如何构建语义缩放. 为什么需要语义缩放 如果用过Windows 8系统的开发者都知道在Win ...

  4. codeforces #262 DIV2 C称号Present(二分法+贪婪)

    职务地址:http://codeforces.com/contest/460/problem/C 这个题是用二分枚举最小值.然后推断是否能在规定的次数内使得全部的数都达到这个值.推断的时候要用贪心的方 ...

  5. FFT 的C 语言

    FFT 的C 语言 说好的C 语言实现.必须搞定它! 理论介绍: http://blog.csdn.net/cinmyheart/article/details/39052739 这里有之前matla ...

  6. jQuery.form Ajax无刷新上传错误 (jQuery.handleError is not a function) 解决方案

    今天,随着ajaxfileupload时间firebug财报显示,"jQuery.handleError is not a function"错误.因为一旦使用jQuery.for ...

  7. css优先级机制说明

    原文:css优先级机制说明 首先说明下样式的优先级,样式有三种: 1. 外部样式(External style sheet) 示例: <!-- 外部样式 bootstrap.min.css -- ...

  8. A/B测试评测

    A/B测试评测 A/B测试在各类网站设计中已经是比较常见的,本文着重讲讲A/B测试在应用推送领域的作用. 目前国外开通A/B测试的推送服务商只有swrve,而国内的个推也在前不久发布的smart pu ...

  9. MIT Introduction to Computer Science and Programming (Lesson one )

    MIT Introduction to Computer Science and Programming (Lesson one ) 这篇文是记载 MIT 计算机科学及编程导论 第一集 的笔记 Les ...

  10. FormsAuthentication登录ReturnUrl使用绝对路径

    ASP.NET]更简单的方法:FormsAuthentication登录ReturnUrl使用绝对路径   被这个问题困扰多年,今天终于找到了更简单的解决方法,分享一下. 问题场景 假设我们在i.cn ...