Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # this program takes the XML code that we created in exercise 2
- # and converts it back to JSON and stores it in a file.
- # import the xml converter and the json library
- import xmltodict, json
- # open the XML.txt file
- file = open("XML.txt", "r")
- # open the JSON.txt file to write to
- file_output = open("JSON.txt", "w")
- # load the XML data into a variable
- data = file.read()
- # create a dictionary
- d = {}
- # take the XML variable data and convert it to a python dictionary
- d = xmltodict.parse(data)
- # take the dictionary and convert it to JSON format
- json_format = json.dumps(d)
- # write the json data to a text file
- file_output.write(json_format)
- # we are all done now it is time to close both files
- file.close()
- file_output.close()
Advertisement
Add Comment
Please, Sign In to add comment