XMLtoCSV.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import xml.etree.ElementTree as Xet
  2. import os
  3. import csv
  4. #Passo al parser i file xml
  5. xml_file_name = '/Users/federicaspinelli/Google Drive/OVI-CNR/XML/XML SCHEDE OA/000-Opere_Gall.Martini/AR20AUT.xml'
  6. tree = Xet.parse(xml_file_name)
  7. #Dichiaro l'elemento radice
  8. root = tree.getroot()
  9. #Dichiaro l'elemento che contiene tutte le risorse da processare. Sara' la base per costruire i path
  10. schede = root.find("schede")
  11. #Dichiaro la lista degli elementi da processare
  12. schedeM = root.findall("schede/scheda")
  13. xmlparseO = Xet.parse('/Users/federicaspinelli/Google Drive/OVI-CNR/XML/XML SCHEDE OA/000-Opere_Comm.Ospedale/AR20AUT.xml')
  14. rootO = xmlparseO.getroot()
  15. schedeO = rootO.findall("schede/scheda")
  16. xmlparseF = Xet.parse('/Users/federicaspinelli/Google Drive/OVI-CNR/XML/XML SCHEDE OA/000-Opere_Datini/AR20AUT.xml')
  17. rootF = xmlparseF.getroot()
  18. schedeF = rootF.findall("schede/scheda")
  19. # Comincio a scrivere il file csv
  20. BIB_data = open('BIB_Data.csv', 'w')
  21. csvwriter = csv.writer(BIB_data)
  22. list_path = []
  23. #Questa funzione ricostruisce i path dei nodi
  24. def iterate(node, path=""):
  25. aut = []
  26. if path:
  27. current_path = path + "/" + node.tag
  28. else:
  29. current_path = node.tag
  30. path = current_path
  31. for child in node:
  32. iterate(child, path=current_path)
  33. obj = cell(path)
  34. aut.append(obj)
  35. def cell(path):
  36. nd = root.find(path)
  37. res = nd.text
  38. tag = nd.tag
  39. #Dato che ho memorizzato tutti i path a partire da root elimino la parte del path che e' già dichiarata per il mio oggetto
  40. new_path = path.replace('schede/scheda/', '')
  41. #Elimino i caratteri non convenzionali dalle stringhe.
  42. #Operazione necessaria per non avere celle con \n\t, che in realta' sono celle vuote
  43. str = res.rstrip()
  44. #Se la stringa non è vuota memorrizzo nell'array tag e path corrspondenti
  45. if str != "":
  46. list_path.append([tag, new_path])
  47. return str
  48. #Lancio la funziona iterate su schede per ottenere tutti i tag contenuti in schede con i percorsi associati
  49. iterate(schede)
  50. #Elimino i duplicati dall'array list_path e salvo un array params con i soli tag, mi servira' per costruire la prima riga del csv
  51. #array andra' a contenere tutti tag distinti con i relativi percorsi
  52. array = []
  53. params = []
  54. seen = set()
  55. for item in list_path:
  56. t = tuple(item)
  57. if t not in seen:
  58. params.append(item[0])
  59. array.append(item)
  60. seen.add(t)
  61. #Scrivo la prima riga del csn con i nomi dei tag
  62. csvwriter.writerow(params)
  63. #Controllo che il valore trovato non sia nullo, cioe' quel tag non ha un valore associato.
  64. #In questo caso la cella deve essere vuota, quindi dichiaro un carattere vuoto
  65. #altrimenti dichiaro il testo associato all'elemento
  66. def check(val):
  67. if val is None:
  68. res = " "
  69. else:
  70. res = val.text
  71. return res
  72. #Costruisco ciascuna riga del csv
  73. def build_rows(schede):
  74. for scheda in schede:
  75. #gg è una riga del csv
  76. gg = []
  77. #Per ogni elemento di array costruisco una cella della riga
  78. for x in range(len(array)):
  79. par = array[x][0]
  80. #Riprendo il path e dichiaro il valore associato
  81. path = array[x][1]
  82. value = scheda.find(path)
  83. #Faccio un controllo sul valore attraverso la funzione check
  84. r = check(value)
  85. #Appendo il valore all'array gg
  86. gg.append(r)
  87. #Scrivo la riga nel file csv
  88. csvwriter.writerow(gg)
  89. build_rows(schedeM)
  90. build_rows(schedeO)
  91. build_rows(schedeF)
  92. BIB_data.close()