inflab4/extra1.py

19 lines
508 B
Python
Raw Permalink Normal View History

2023-07-20 22:08:19 +00:00
import xmltodict
import json
def xml_to_json(xml: str, pretty_formatting: bool) -> str:
xml_dict = xmltodict.parse(xml)
indent = 4 if pretty_formatting else None
json_str = json.dumps(xml_dict, ensure_ascii=False, indent=indent)
return json_str
if __name__ == "__main__":
file = open('schedule.xml', encoding='utf8')
content = file.read()
result = xml_to_json(content, True)
file = open('schedule.json', 'w', encoding='utf8')
file.writelines(result)
print(result)