python json unicode utf-8处理总结
1.直接输出字典中文
在python中经常遇见直接print dict(字典),或者dict转json,但是没有给特定的参数,然后打印json字符串,输出的中文就成了unicode码的情况,如下:
d = {'name': '张三', 'age': '1'}
print d
jd = json.dumps(d)
print jd
输出结果为:
{'age': '1', 'name': '\xe5\xbc\xa0\xe4\xb8\x89'}
{"age": "1", "name": "\u5f20\u4e09"}
这种情况怎么办呢?
要将字典中的中文正确的输出,可以将d转换成json字符串,转换时使用json.dumps(d, ensure_ascii=False, encoding='utf-8'))
d = {'name': '张三', 'age': '1'}
print d
jd = json.dumps(d, ensure_ascii=False, encoding='utf-8'))
print jd
输出结果为:
{'age': '1', 'name': '\xe5\xbc\xa0\xe4\xb8\x89'}
{"age": "1", "name": "张三"}
参数ensure_ascii=False不能少, encoding可以省略,因为默认就是encoding='utf-8'
关于参数ensure_ascii的解释:
If ``ensure_ascii`` is true (the default), all non-ASCII characters in the
output are escaped with ``\uXXXX`` sequences, and the result is a ``str``
instance consisting of ASCII characters only. If ``ensure_ascii`` is
``False``, some chunks written to ``fp`` may be ``unicode`` instances.
This usually happens because the input contains unicode strings or the
``encoding`` parameter is used. Unless ``fp.write()`` explicitly
understands ``unicode`` (as in ``codecs.getwriter``) this is likely to
cause an error.
关于参数encoding的解释:
``encoding`` is the character encoding for str instances, default is UTF-8.
2.用python自带的json库将json转换成字典输出,输出是unicode码
在用json.loads(json_str)将json_str字符串转换成字典时,字典中的内容是unicode码,具体如下:
ud = json.loads(jd, encoding='utf-8')
print ud
输出结果:
{u'age': u'1', u'name': u'\u5f20\u4e09'}
字典中的字符串都带的u,要想去掉u,有两种办法
a.使用yaml库的yaml.safe_load(jd)
import yaml
d = {'name': '张三', 'age': '1'}
print d
jd = json.dumps(d, ensure_ascii=False, encoding='utf-8'))
ud = json.loads(jd, encoding='utf-8')
print ud
ud = yaml.safe_load(jd, encoding='utf-8')
print ud
结果输出为:
{u'age': u'1', u'name': u'\u5f20\u946b'}
{'age': '1', 'name': u'\u5f20\u946b'}
视觉明锐的同学可能发现第二个name的值前还是有u,也就是说他是unicode码。的确是的,上面的第1点已经说明了,直接打印字典,字典里面的中文就是乱码的,但是为什么是unicode码,需要更深一步分析,也希望知道的朋友不吝留言告知,谢谢。
b 递归实现转码函数自己去将json.loads()返回的字典从unicode码转成自己想要的码,实现如下:
def byteify(input, encoding='utf-8'):
if isinstance(input, dict):
return {byteify(key): byteify(value) for key, value in input.iteritems()}
elif isinstance(input, list):
return [byteify(element) for element in input]
elif isinstance(input, unicode):
return input.encode(encoding)
else:
return input
使用示例:
d = {'name': '张三', 'age': '1'}
print d
jd = json.dumps(d, ensure_ascii=False, encoding='utf-8'))
ud = json.loads(jd, encoding='utf-8')
print ud
ud = byteify(ud)
print ud
print ud['name']
输出结果如下:
{u'age': u'1', u'name': u'\u5f20\u946b'}
{'age': '1', 'name': '\xe5\xbc\xa0\xe9\x91\xab'}
张三
这次是彻底的将json.loads()返回的字典转换码成了utf-8,至于输出为什么是乱码?别忘了,开头第一点说的,直接print字典,中文是会乱码的,但是print ud['name'] 就会正常显示中文'张三'。
参考来源:https://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-from-json
作者:llicety
链接:https://www.jianshu.com/p/90ecc5987a18
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
python json unicode utf-8处理总结的更多相关文章
- python json基础学习01
# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' import json #全称(javascript object ...
- python json数据的转换
1 Python数据转json字符串 import json json_str = json.dumps(py_data) 参数解析: json_str = json.dumps(py_data,s ...
- python大法好——python json
Python JSON 本章节我们将为大家介绍如何使用 Python 语言来编码和解码 JSON 对象. JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式, ...
- Python: json模块实例详解
ref:https://www.jianshu.com/p/e29611244810 https://www.cnblogs.com/qq78292959/p/3467937.html https:/ ...
- python学习笔记——python JSON
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 1.JSON 函数 使用 JSON 函数需要导入 json 库:import json ...
- Python json使用
转自:https://www.cnblogs.com/wangyayun/p/6699184.html?utm_source=tuicool&utm_medium=referral 使用Pyt ...
- python json.dumps() 中文乱码问题
python json.dumps() 中文乱码问题 python 输出一串中文字符,在控制台上(控制台使用UTF-8编码)通过print 可以正常显示,但是写入到文件中之后,中文字符都输出成as ...
- python中unicode和str的组合
python中unicode对象和str对象拼接在一起,会自动将str对象转换成unicode对象 即:a="aa" b=u"bb" c=a+b type(c) ...
- python json库
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,易于人阅读和编写. 1.json库的使用 使用 JSON 函数需要导入 json 库:import jso ...
随机推荐
- android 动态设置TextView值,例:金额增加
一说到动态递增设置TextView值,很多人应该马上就想到起个线程,让后在线程中睡眠指定时间,使用handler发送消息更新TextView值! 这样是实现了动态递增设置TextView值但是效率不咋 ...
- 真香警告!扩展 swagger支持文档自动列举所有枚举值
承接上篇文章 <一站式解决使用枚举的各种痛点> 文章最后提到:在使用 swagger 来编写接口文档时,需要告诉前端枚举类型有哪些取值,每次增加取值之后,不仅要改代码,还要找到对应的取值在 ...
- (mysql)数据库笔记
一.数据库的特点: a.实现数据共享 b.采用特定的数据类型. c.具有较高的数据独立性 d.具有统一的数据控制功能. 二.mysql的优势: a.速度:运行速度快 b.价格:mysql对多数个人来 ...
- Java内存区域与内存溢出异常——深入理解Java虚拟机 笔记一
Java内存区域 对比与C和C++,Java程序员不需要时时刻刻在意对象的创建和删除过程造成的内存溢出.内存泄露等问题,Java虚拟机很好地帮助我们解决了内存管理的问题,但深入理解Java内存区域,有 ...
- java十大排序
0.1 算法分类 十种常见排序算法可以分为两大类: 比较类排序:通过比较来决定元素间的相对次序,由于其时间复杂度不能突破O(nlogn),因此也称为非线性时间比较类排序. 非比较类排序:不通过比较来决 ...
- JDK 安装与环境变量配置
JDK官网下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 1.下载jd ...
- P2444 [POI2000]病毒 AC自动机
P2444 [POI2000]病毒 #include <bits/stdc++.h> using namespace std; ; struct Aho_Corasock_Automato ...
- 函数的不同调用方式决定了this的指向不同
一.函数的不同调用方式决定了this的指向不同,一般指向调用者 1.普通函数 this指向window的调用者 function fn(){ console.l ...
- 获取访问用户的客户端IP(适用于公网与局域网).
/** * 获取访问用户的客户端IP(适用于公网与局域网). */ public final String getIpAddr(final HttpServletRequest requ ...
- C# 使用Word模板导出数据
使用NPOI控件导出数据到Word模板中方式: 效果如下: Word模板: 运行结果: 实现如下: Student.cs using System; using System.Collections. ...