iamarf

#LACA add-data

Apr 2nd, 2019
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.14 KB | None | 0 0
  1. # add-data.python-csv
  2.  
  3. # 18 marzo
  4.  
  5. # Copyright 2019 Andreas R. Formiconi
  6.  
  7. #    This program is free software: you can redistribute it and/or modify
  8. #    it under the terms of the GNU General Public License as published by
  9. #    the Free Software Foundation, either version 3 of the License, or
  10. #    (at your option) any later version.
  11.  
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16.  
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <https://www.gnu.org/licenses/gpl.txt>
  19.  
  20.  
  21. # Trasforma un CSV per i nodi di accoglienza esportato da bastamortinelmediterraneo
  22. # in un CSV da caricare in Ushahidi
  23.  
  24. # 18 marzo 2019
  25.  
  26. # Legge un file CSV scaricato dal modulo wordpress di
  27. # bastamortinelmediterraneo creato da Caterina Cirri,
  28. # ne estrae e manipola alcune voci per scriverle in
  29. # un file CSV idoneo ad essere caricato in Ushahidi.
  30.  
  31. # I campi scritti nel CSV di destinazione sono
  32.  
  33. # 1  "Nome-nodo",
  34. # 2  "Descrizione",
  35. # 3  "Latitudine",
  36. # 4  "Longitudine",
  37. # 5  "Telefono",
  38. # 6  "Email",
  39. # 7  "Nome",
  40. # 8  "Cognome",
  41. # 9  "Ind-via-num",
  42. # 10 "Localita",
  43. # 11 "Provincia",
  44. # 12 "CAP",
  45. # 13 "Offerta",
  46. # 14 "Note"
  47.  
  48. # Questi rappresentano un sottoinsieme comune ragionevole
  49. # intersezione fra i dati di Caterina e quelli che formano
  50. # una entry dei nodi di accoglienza nella nostra crowdmap
  51.  
  52. # I campi letti nel CSV in ingresso bastamortinelmediterraneo
  53. # sono invece
  54.  
  55. # 1  "Nome (Prefisso)"
  56. # 2  "Nome"
  57. # 3  "Nome (Medio)"
  58. # 4  "Cognome"
  59. # 5  "Nome (Suffisso)"
  60. # 6  "Telefono"
  61. # 7  "Email"
  62. # 8  "Ind-via-num"
  63. # 9  "Ind-via-num-2"
  64. # 10 "Localita"
  65. # 11 "Provincia"
  66. # 12 "CAP"
  67. # 13 "Nazione"
  68. # 14 "Acc-vis-med"
  69. # 15 "Acc-pra-bur"
  70. # 16 "Sos-leg"
  71. # 17 "Sos-psi"
  72. # 18 "Acc-fam"
  73. # 19 "Ins-ita"
  74. # 20 "Lez-scu-gui
  75. # 21 "Pra-cen"
  76. # 22 "Osp-eme"
  77. # 23 "Altro"
  78. # 24 "Note"
  79. # 25 "Desidero ricevere il notiziario delle vostre attività"
  80. # 26 "Utilizzando questo modulo accetti la memorizzazione e la gestione dei tuoi dati da parte di questo sito web ai sensi dell’art. 13 del Regolamento europeo 679/2016. (richiesto)"
  81. # 27 "Creato da (ID dell'utente)"
  82. # 28 "ID Registrazione"
  83. # 29 "Data della registrazione"
  84. # 30 "URL sorgente"
  85. # 31 "ID transazione"
  86. # 32 "Importo del pagamento"
  87. # 33 "data pagamento"
  88. # 34 "Stato del pagamento"
  89. # 35 "ID articolo"
  90. # 36 "User Agent"
  91. # 37 "IP utente"
  92.  
  93. # Di questi ultimi, i campi 14-22
  94.  
  95. # 14 "Acc-vis-med"
  96. # 15 "Acc-pra-bur"
  97. # 16 "Sos-leg"
  98. # 17 "Sos-psi"
  99. # 18 "Acc-fam"
  100. # 19 "Ins-ita"
  101. # 20 "Lez-scu-gui
  102. # 21 "Pra-cen"
  103. # 22 "Osp-eme"
  104.  
  105. # vengono condensati dallo script nel campo 13 "Offerta"
  106. # del CVS in uscita.
  107.  
  108. # Usato
  109. # https://realpython.com/python-csv/
  110. # https://stackoverflow.com/questions/49527005/python-set-a-quotechar-for-csv-dictwriter
  111. #https://stackoverflow.com/questions/3348460/csv-file-written-with-python-has-blank-lines-between-each-row
  112. # https://www.thegeekstuff.com/2013/06/python-list/?utm_source=feedly per aggiungere un elemento al principio di una lista
  113.  
  114. # La seconda riga deve essere fatta da
  115. # ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
  116. # perché il primo record dopo l'intestazione se lo perde
  117.  
  118.  
  119.  
  120.  
  121. import csv
  122. import random
  123.  
  124. with open('data-solidarieta-2.csv') as csv_rfile, open('data-for-ushahidi.csv', mode='w+', newline='') as csv_wrfile:
  125.     csv_reader = csv.DictReader(csv_rfile)
  126.     fieldnames_write = ['Telefono', 'Email', 'Nome', 'Cognome', 'Ind-via-num', 'Localita', 'Provincia', 'CAP', 'Offerta', 'Note']
  127.     separator = ','
  128.     quote = '"'
  129.     csv_writer = csv.DictWriter(csv_wrfile, fieldnames = fieldnames_write, delimiter = separator, quotechar = quote, quoting=csv.QUOTE_NONNUMERIC)
  130.     csv_writer.writeheader()
  131.  
  132.     line_count = 0
  133.    
  134.     for row in csv_reader:
  135.         if line_count == 0:
  136.             print(f'Column names are {", ".join(row)}')
  137.             line_count += 1
  138.         else:
  139.             offerta = ''
  140.             if row['Acc-vis-med']:
  141.                 offerta = offerta + row['Acc-vis-med'] + ', '
  142.             if row['Acc-pra-bur']:
  143.                 offerta = offerta + row['Acc-pra-bur'] + ', '  
  144.             if row['Sos-leg']:
  145.                 offerta = offerta + row['Sos-leg'] + ', '
  146.             if row['Sos-psi']:
  147.                 offerta = offerta + row['Sos-psi'] + ', '
  148.             if row['Acc-fam']:
  149.                 offerta = offerta + row['Acc-fam'] + ', '
  150.             if row['Ins-ita']:
  151.                 offerta = offerta + row['Ins-ita'] + ', '
  152.             if row['Lez-scu-gui']:
  153.                 offerta = offerta + row['Lez-scu-gui'] + ', '
  154.             if row['Pra-cen']:
  155.                 offerta = offerta + row['Pra-cen'] + ', '
  156.             if row['Osp-eme']:
  157.                 offerta = offerta + row['Osp-eme'] + ', '
  158.             offerta = offerta[:-2]
  159.             csv_writer.writerow({'Telefono': row['Telefono'], 'Email': row['Email'],'Nome': row['Nome'], 'Cognome': row['Cognome'],  'Ind-via-num': row['Ind-via-num'], 'Localita': row['Localita'], 'Provincia': row['Provincia'], 'CAP': row['CAP'], 'Offerta': offerta, 'Note': row['Note']})
  160.             line_count += 1
  161.    
  162.     print(f'Processed {line_count} lines.')
  163.    
  164. with open('data-for-ushahidi.csv') as csv_rfile, open('data-for-ushahidi-all.csv', mode='w+', newline='') as csv_wrfile:
  165.     csv_reader = csv.reader(csv_rfile, delimiter=',')
  166.     csv_writer = csv.writer(csv_wrfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
  167.     line_count = 0
  168.  
  169.     for row in csv_reader:
  170.  
  171.         if line_count == 0:
  172.             # Piazza Stazione Firenze
  173.             lat0 = 43.776272983621894
  174.             lon0 = 11.247024534386583
  175.             # Vinci: 43.769053, 10.919953
  176.             # Vaglia: 43.910595, 11.278009
  177.             latmax = 43.910595
  178.             latmin = lat0 - (latmax - lat0)
  179.             lonmin = 10.919953
  180.             lonmax = lon0 + (lon0 - lonmin)
  181.             row.insert(0,"Nome-nodo")
  182.             row.insert(1,"Descrizione")
  183.             row.insert(2,"Latitudine")
  184.             row.insert(3,"Longitudine")
  185.             #row.pop(14)
  186.             csv_writer.writerow(row)
  187.             line_count += 1
  188.         else:
  189.             row.insert(0,"Rete di solidarietà")
  190.             row.insert(1,"Clicca per vedere la scheda completa a fianco")
  191.             row.insert(2,random.uniform(latmin, latmax))
  192.             row.insert(3,random.uniform(lonmin, lonmax))
  193.             #row.pop(14)
  194.             csv_writer.writerow(row)
  195.             line_count += 1
  196.  
  197.     print(f'Processed {line_count} lines.')
Add Comment
Please, Sign In to add comment