活动地址:CSDN21天学习挑战赛
这节内容学习了JSON基础概述,JSON模块,XML文件与JSON文件互转,解析JSON文件。现在进行复盘及应用。
- python_str={
- "season":
- [
- {"spring":{"fruit":"pineapple"}},
- {"summer":{"fruit":["watermelon","peach"]}},
- {"fall":{"fruit": "apple"}},
- {"winter":{"fruit": "orange"}}
- ]
- }
-
- json_str=json.dumps(python_str)
-
- #输出
- {"season": [{"spring": {"fruit": "pineapple"}}, {"summer": {"fruit": ["watermelon", "peach"]}}, {"fall": {"fruit": "apple"}}, {"winter": {"fruit": "orange"}}]}
-
-
- json_str=json.dumps(python_str,sort_keys=True,indent=3)
- print(json_str)
-
- #输出
- {
- "season": [
- {
- "spring": {
- "fruit": "pineapple"
- }
- },
- {
- "summer": {
- "fruit": [
- "watermelon",
- "peach"
- ]
- }
- },
- {
- "fall": {
- "fruit": "apple"
- }
- },
- {
- "winter": {
- "fruit": "orange"
- }
- }
- ]
- }
-
- 进程已结束,退出代码0

![]()
json.dump(python_str,open("./json.file.json","w"),sort_keys=True,indent=3)
存储结果:


-
- python_str=json.loads(json_str)
-
-
- #输出
- {'season': [{'spring': {'fruit': 'pineapple'}}, {'summer': {'fruit': ['watermelon', 'peach']}}, {'fall': {'fruit': 'apple'}}, {'winter': {'fruit': 'orange'}}]}
-
- python_str=json.load(open("./json.file.json","r"))
- print(python_str)
- #输出
- {'season': [{'spring': {'fruit': 'pineapple'}}, {'summer': {'fruit': ['watermelon', 'peach']}}, {'fall': {'fruit': 'apple'}}, {'winter': {'fruit': 'orange'}}]}

安装模块——
pip install xmltodict
新建一个1.xml文件:

转换代码:
- import json
- import xmltodict
- def xml_to_json(xml_str):
- xml_parse=xmltodict.parse(xml_str)
- json_str=json.dumps(xml_parse,indent=2)
- return json_str
- xml_path='./1.xml'
- f=open(xml_path,"r")
- xml_file=f.read()
- with open('./1.jsonfile.json',"w") as newf:
- newf.write(xml_to_json(xml_file))
输出:

转换代码:
- import json
- import xmltodict
- def json_to_xml(python_dir):
- xml_str=xmltodict.unparse(python_dir)
- return xml_str
-
- json_path="./1.jsonfile.json"
- f=open(json_path,"r")
- json_file=f.read()
- python_dir=json.loads(json_file)
- with open("./2.xml","w") as fp:
- fp.write(json_to_xml(python_dir))
-
- #原内容
- {
- "action": {
- "@year": "3/08/2022",
- "season": [
- "summer",
- "winter"
- ],
- "incident": "miss"
- }
- }
- #写入xml文件内容
- "1.0" encoding="utf-8"?>
"3/08/2022">summer winter miss
- import json
- python_str={
- "season":
- [
- {"spring":[{"fruit":"pineapple"}]},
- {"summer":[{"fruit":["watermelon","peach"]}]},
- {"fall":[{"fruit": "apple"}]},
- {"winter":[{"fruit": "orange"}]}
- ]
- }
- #定位数据节点
- #打印字典所有key
- print(python_str.keys())
- #打印字典所有value
- print(python_str.values())
- print(python_str["season"][0])
- print(python_str["season"][1])
-
- #输出
- dict_keys(['season'])
- dict_values([[{'spring': [{'fruit': 'pineapple'}]}, {'summer': [{'fruit': ['watermelon', 'peach']}]}, {'fall': [{'fruit': 'apple'}]}, {'winter': [{'fruit': 'orange'}]}]])
- {'spring': [{'fruit': 'pineapple'}]}
- {'summer': [{'fruit': ['watermelon', 'peach']}]}


- import json
-
- # 1、JSON文件转换为Python对象
- python_obj = json.load(open('test.json', 'r'))
- print(python_obj)
- print(type(python_obj))
-
- print("-" * 20)
- # 2、解析json文件
-
- # 输出cours节点e下的数据
- print(python_obj['student']['course']['name']) # 输出name
- print(python_obj['student']['course']['score']) # 输出score
-
- # 输出info节点下的数据
- print(python_obj['student']['info']['sex']) # 输出sex
- print(python_obj['student']['info']['name']) # 输出name
-
- # 输出stid节点下的数据
- print(python_obj['student']['stid']) # 输出stid