CSV_to_CSV_associate_id.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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('mergeASPO.csv', 'w')
  9. csvwriter = csv.writer(merged_data)
  10. '''params = ['codice', 'sigla']'''
  11. params = ['PLACE NAME', 'ID ASPO', 'ID RESTORE']
  12. csvwriter.writerow(params)
  13. '''
  14. def get_idR(place):
  15. link_file = open('/Users/alessiaspadi/Documents/RESTORE/temp_ovi/prove/altro/Luoghi.csv')
  16. reader = csv.DictReader(link_file)
  17. for row in reader:
  18. name_place = row['name_place'].lower()
  19. pp = place.lower()
  20. if name_place == pp:
  21. return row['id']
  22. '''
  23. def get_aspoId(place):
  24. link_file = open('/Users/alessiaspadi/Documents/RESTORE/temp_ASPO/Luoghi/Associa/Luoghi_ASPO.csv')
  25. reader = csv.DictReader(link_file)
  26. for row in reader:
  27. id_place = row['ID ASPO']
  28. id_restore = row['ID RESTORE']
  29. if place == id_place:
  30. return id_restore
  31. merge_file = open('/Users/alessiaspadi/Documents/RESTORE/temp_ASPO/Luoghi/Associa/data_eac_geo.csv')
  32. reader = csv.DictReader(merge_file)
  33. for row in reader:
  34. line = []
  35. id_luogo = row['ID ASPO']
  36. nome_luogo = row['PLACE NAME']
  37. id_aspo = get_aspoId(id_luogo)
  38. line.append(nome_luogo)
  39. line.append(id_luogo)
  40. if id_aspo is not None:
  41. line.append(id_aspo)
  42. else:
  43. line.append("")
  44. csvwriter.writerow(line)
  45. merged_data.close()