danielperezunlpam

xmltodict

Nov 26th, 2020
553
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import xmltodict
  2. import pprint
  3.  
  4.  
  5. if __name__ == '__main__':
  6.     with open("xml_folder/country.xml") as ifile:
  7.         text = ifile.read()
  8.         # print(text)
  9.         d = xmltodict.parse(text)
  10.         # pprint.pprint(d)
  11.  
  12.         for country in d["countries"]["country"]:
  13.             country["#text"] = country["#text"].upper()
  14.             print(country["#text"])
  15.  
  16.         with open("xml_folder/country_3.xml", "w") as ofile:
  17.             text = xmltodict.unparse(d, pretty=True)
  18.             ofile.write(text)
  19.  
RAW Paste Data