CSV_to_CSV_associazione_toponimi.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import csv
  2. import codecs
  3. import pandas as pd
  4. import re
  5. import os
  6. import io
  7. import tokenize
  8. merged_data = open('ovi_table.csv', 'w')
  9. csvwriter = csv.writer(merged_data)
  10. params = ['sigla', 'toponimo', 'TGN', 'ID RESTORE']
  11. csvwriter.writerow(params)
  12. def get_IdRestore(tgn):
  13. link_file = open('/Users/alessiaspadi/Documents/RESTORE/temp_ASPO/Luoghi/Associa/Toponimi.csv')
  14. reader = csv.DictReader(link_file)
  15. for row in reader:
  16. getty = row['TGN']
  17. id_restore = row['ID RESTORE']
  18. if tgn == getty:
  19. return id_restore
  20. def get_idR(place):
  21. link_file = open('/Users/alessiaspadi/Documents/RESTORE/temp_ASPO/Luoghi/Associa/Toponimi.csv')
  22. reader = csv.DictReader(link_file)
  23. for row in reader:
  24. name_place = row['TOPONIMO'].lower()
  25. pp = place.lower()
  26. if name_place == pp:
  27. return row['ID RESTORE']
  28. def get_TGN(aspo):
  29. link_file = open('/Users/alessiaspadi/Documents/RESTORE/temp_ASPO/Luoghi/Associa/Luoghi_ASPO.csv')
  30. reader = csv.DictReader(link_file)
  31. for row in reader:
  32. getty = row['getty_code']
  33. id_aspo = row['ID ASPO']
  34. if aspo == id_aspo:
  35. return getty
  36. merge_file = open('/Users/alessiaspadi/Documents/RESTORE/temp_ASPO/Luoghi/Associa/OVI.csv')
  37. reader = csv.DictReader(merge_file)
  38. for row in reader:
  39. line = []
  40. sigla = row['sigla']
  41. toponimo = row['toponimo']
  42. tgn = row['tgn']
  43. id_restore = get_IdRestore(tgn)
  44. line.append(sigla)
  45. line.append(toponimo)
  46. line.append(tgn)
  47. if id_restore is not None:
  48. line.append(id_restore)
  49. else:
  50. line.append("")
  51. csvwriter.writerow(line)
  52. merged_data.close()