Python中xml、字典、EasyDict、json格式相互转换(包含json格式化和xml格式化)
依赖包
- json
- xmltodict
xml转字典
import xmltodict
xmlstr ="""
<student>
<stid>10213</stid>
<info>
<name>name</name>
<sex>male</sex>
</info>
<course>
<name>math</name>
<score>90</score>
</course>
</student>
"""
#parse是的xml解析器
dict_ = xmltodict.parse(xmlstr)
print(dict_)
OrderedDict([('student', OrderedDict([('stid', '10213'), ('info', OrderedDict([('name', 'name'), ('sex', 'male')])), ('course', OrderedDict([('name', 'math'), ('score', '90')]))]))])
字典转EasyDict
import xmltodict
xmlstr ="""
<student>
<stid>10213</stid>
<info>
<name>name</name>
<sex>male</sex>
</info>
<course>
<name>math</name>
<score>90</score>
</course>
</student>
"""
#parse是的xml解析器
dict_ = xmltodict.parse(xmlstr)
#转EasyDict
from easydict import EasyDict
dict_ = EasyDict(dict_)
print(dict_)
{'student': {'stid': '10213', 'info': {'name': 'name', 'sex': 'male'}, 'course': {'name': 'math', 'score': '90'}}}
字典转json
最简版
import json
dict_ = {
'name': 'Jack',
'age': 22,
'skills': ['Python', 'Java', 'C++', 'Matlab'],
'major': '计算机技术',
'english': '英语六级',
'school': 'WIT'
}
#json库dumps()是将dict转化成json格式,loads()是将json转化成dict格式。
#dumps()方法的ident=1,格式化json
jsonstr = json.dumps(dict_)
print(jsonstr)
{"name": "Jack", "age": 22, "skills": ["Python", "Java", "C++", "Matlab"], "major": "\u8ba1\u7b97\u673a\u6280\u672f", "english": "\u82f1\u8bed\u516d\u7ea7", "school": "WIT"}
很明显中文字符被转化了,于是使用:ensure_ascii=False
import json
dict_ = {
'name': 'Jack',
'age': 22,
'skills': ['Python', 'Java', 'C++', 'Matlab'],
'major': '计算机技术',
'english': '英语六级',
'school': 'WIT'
}
#json库dumps()是将dict转化成json格式,loads()是将json转化成dict格式。
#dumps()方法的ident=1,格式化json
jsonstr = json.dumps(dict_,ensure_ascii=False)
print(jsonstr)
{"name": "Jack", "age": 22, "skills": ["Python", "Java", "C++", "Matlab"], "major": "计算机技术", "english": "英语六级", "school": "WIT"}
加入行缩进
import json
dict_ = {
'name': 'Jack',
'age': 22,
'skills': ['Python', 'Java', 'C++', 'Matlab'],
'major': '计算机技术',
'english': '英语六级',
'school': 'WIT'
}
#json库dumps()是将dict转化成json格式,loads()是将json转化成dict格式。
#dumps()方法的ident=1,格式化json
jsonstr = json.dumps(dict_,indent=2) #indent=2 表示行缩进=2
print(jsonstr)
{
"name": "Jack",
"age": 22,
"skills": [
"Python",
"Java",
"C++",
"Matlab"
],
"major": "\u8ba1\u7b97\u673a\u6280\u672f",
"english": "\u82f1\u8bed\u516d\u7ea7",
"school": "WIT"
}
很明显中文字符被转化了,于是使用:ensure_ascii=False
import json
dict_ = {
'name': 'Jack',
'age': 22,
'skills': ['Python', 'Java', 'C++', 'Matlab'],
'major': '计算机技术',
'english': '英语六级',
'school': 'WIT'
}
#json库dumps()是将dict转化成json格式,loads()是将json转化成dict格式。
#dumps()方法的ident=1,格式化json
jsonstr = json.dumps(dict_,indent=2,ensure_ascii=False) #indent=2 表示行缩进=2
print(jsonstr)
{
"name": "Jack",
"age": 22,
"skills": [
"Python",
"Java",
"C++",
"Matlab"
],
"major": "计算机技术",
"english": "英语六级",
"school": "WIT"
}
json转字典
json.loads(json串),将json字符串转化成字典
import json
jsonstr = """
{
"name": "Jack",
"age": 22,
"skills": [
"Python",
"Java",
"C++",
"Matlab"
],
"major": "计算机技术",
"english": "英语六级",
"school": "WIT"
}
"""
dict_ = json.loads(jsonstr)
print(dict_)
{'name': 'Jack', 'age': 22, 'skills': ['Python', 'Java', 'C++', 'Matlab'], 'major': '计算机技术', 'english': '英语六级', 'school': 'WIT'}
字典转xml
import xmltodict
dict_ = {'student': {'course': {'name': 'math', 'score': '90'},
'info': {'sex': 'male', 'name': 'name'}, 'stid': '10213'}}
xmlstr = xmltodict.unparse(dict_)
print(xmlstr)
<?xml version="1.0" encoding="utf-8"?>
<student><course><name>math</name><score>90</score></course><info><sex>male</sex><name>name</name></info><stid>10213</stid></student>
XML格式化(美化)
import xmltodict
dict_ = {'student': {'course': {'name': 'math', 'score': '90'},
'info': {'sex': 'male', 'name': 'name'}, 'stid': '10213'}}
xmlstr = xmltodict.unparse(dict_)
from xml.dom import minidom
xml = minidom.parseString(xmlstr)
xml_pretty_str = xml.toprettyxml()
print(xml_pretty_str)
<?xml version="1.0" encoding="utf-8"?>
<student>
<course>
<name>math</name>
<score>90</score>
</course>
<info>
<sex>male</sex>
<name>name</name>
</info>
<stid>10213</stid>
</student>
参考资料
- Python中xml和json格式相互转换操作示例:https://www.jb51.net/article/152118.htm
- Python json读写方式和字典相互转化:https://www.jb51.net/article/184983.htm
- Python如何优雅的格式化XML 【Python XML Format】: https://blog.csdn.net/qq_41958123/article/details/105357692
评论 (0)