BiblioDat_XML_to_CSV.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import xml.etree.ElementTree as Xet
  2. from typing import Dict, Any
  3. import pandas as pd
  4. import os
  5. import csv
  6. from xml.dom import minidom
  7. import sys
  8. import re
  9. xml_file_name = '/Users/alessiaspadi/Documents/RESTORE/temp_ovi/BiblioDatini.xml'
  10. tree = Xet.parse(xml_file_name)
  11. root = tree.getroot()
  12. biblio = root.findall("Biblio")
  13. Datini_data = open('Datini_Data.csv', 'w')
  14. csvwriter = csv.writer(Datini_data)
  15. elemList = []
  16. xmlTree = Xet.parse(xml_file_name)
  17. for elem in root.iter():
  18. elemList.append(elem.tag)
  19. elemList = list(set(elemList))
  20. elemList.remove("dataroot")
  21. elemList.remove("Biblio")
  22. param = elemList
  23. csvwriter.writerow(param)
  24. #param = ["recno", "titolo", "descrizione", "segnatura", "sigla"]
  25. def cell(p, arr):
  26. if arr.find(p) is None:
  27. res = " "
  28. else:
  29. res = arr.find(p).text
  30. return res
  31. for scheda in biblio:
  32. aut = []
  33. for par in param:
  34. if par == "star_note":
  35. if scheda.find(".//star_note") is None:
  36. r = " "
  37. else:
  38. r = "True"
  39. else:
  40. r = cell(par, scheda)
  41. aut.append(r)
  42. csvwriter.writerow(aut)
  43. Datini_data.close()