我需要在python中将XML转换为没有root的json。这是XML的一个例子
<?xml version="1.0" encoding="UTF-8"?>
<root>
<row>
<Member_ID>926494</Member_ID>
<First_Name>Corissa</First_Name>
<Last_Name>Aguiler</Last_Name>
<Gender>F</Gender>
<Age>39</Age>
<Height>5,3</Height>
<Weight>130</Weight>
<Hours_Sleep>8</Hours_Sleep>
<Calories_Consumed>2501</Calories_Consumed>
<Exercise_Calories_Burned>990</Exercise_Calories_Burned>
<Date>9/11/2017</Date>
</row>
</root>
我需要以下列格式转换为JSON
{
"Member_ID": 926494,
"First_Name": "Corissa",
"Last_Name": "Aguiler",
"Gender": "F",
"Age": 39,
"Height": "5,3",
"Weight": 130,
"Hours_Sleep": 8,
"Calories_Consumed": 2501,
"Exercise_Calories_Burned": 990,
"Date": "9/11/2017"
},
我正在尝试使用xmljson库中的parker约定,但我发现的所有示例都使用字符串作为输入。我似乎无法弄清楚如何传递实际的.xml文件而不是字符串
例如:
from xmljson import parker, Parker
from xml.etree.ElementTree import fromstring
from json import dumps
dumps(parker.data(fromstring('<x><a>1</a><b>2</b></x>')))
'{"a": 1, "b": 2}'
答案 0 :(得分:1)
您可以使用标准xml库将其解析为dict,然后在需要时将dict转储到json:
xml_raw = """<?xml version="1.0" encoding="UTF-8"?>
<root>
<row>
<Member_ID>926494</Member_ID>
<First_Name>Corissa</First_Name>
<Last_Name>Aguiler</Last_Name>
<Gender>F</Gender>
<Age>39</Age>
<Height>5,3</Height>
<Weight>130</Weight>
<Hours_Sleep>8</Hours_Sleep>
<Calories_Consumed>2501</Calories_Consumed>
<Exercise_Calories_Burned>990</Exercise_Calories_Burned>
<Date>9/11/2017</Date>
</row>
<row>
<Member_ID>926494</Member_ID>
<First_Name>Corissa</First_Name>
<Last_Name>Aguiler</Last_Name>
<Gender>F</Gender>
<Age>39</Age>
<Height>5,3</Height>
<Weight>130</Weight>
<Hours_Sleep>8</Hours_Sleep>
<Calories_Consumed>2501</Calories_Consumed>
<Exercise_Calories_Burned>990</Exercise_Calories_Burned>
<Date>9/11/2017</Date>
</row>
</root>"""
import xml.etree.ElementTree as ET
root = ET.fromstring(xml_raw)
xml_dict_list = list()
for row in root.findall('.//row'):
xml_dict = dict()
for item in row.findall('./*'):
xml_dict[item.tag] = item.text
xml_dict_list.append(xml_dict)
print('dict ->', xml_dict_list)
import json
json_str = json.dumps(xml_dict_list)
print('str ->', json_str)
输出:
dict -> [{'Member_ID': '926494', 'First_Name': 'Corissa', 'Last_Name': 'Aguiler', 'Gender': 'F', 'Age': '39', 'Height': '5,3', 'Weight': '130', 'Hours_Sleep': '8', 'Calories_Consumed': '2501', 'Exercise_Calories_Burned': '990', 'Date': '9/11/2017'}, {'Member_ID': '926494', 'First_Name': 'Corissa', 'Last_Name': 'Aguiler', 'Gender': 'F', 'Age': '39', 'Height': '5,3', 'Weight': '130', 'Hours_Sleep': '8', 'Calories_Consumed': '2501', 'Exercise_Calories_Burned': '990', 'Date': '9/11/2017'}]
str -> [{"Member_ID": "926494", "First_Name": "Corissa", "Last_Name": "Aguiler", "Gender": "F", "Age": "39", "Height": "5,3", "Weight": "130", "Hours_Sleep": "8", "Calories_Consumed": "2501", "Exercise_Calories_Burned": "990", "Date": "9/11/2017"}, {"Member_ID": "926494", "First_Name": "Corissa", "Last_Name": "Aguiler", "Gender": "F", "Age": "39", "Height": "5,3", "Weight": "130", "Hours_Sleep": "8", "Calories_Consumed": "2501", "Exercise_Calories_Burned": "990", "Date": "9/11/2017"}]