Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # add-data.python-csv
- # 18 marzo
- # Copyright 2019 Andreas R. Formiconi
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/gpl.txt>
- # Trasforma un CSV per i nodi di accoglienza esportato da bastamortinelmediterraneo
- # in un CSV da caricare in Ushahidi
- # 18 marzo 2019
- # Legge un file CSV scaricato dal modulo wordpress di
- # bastamortinelmediterraneo creato da Caterina Cirri,
- # ne estrae e manipola alcune voci per scriverle in
- # un file CSV idoneo ad essere caricato in Ushahidi.
- # I campi scritti nel CSV di destinazione sono
- # 1 "Nome-nodo",
- # 2 "Descrizione",
- # 3 "Latitudine",
- # 4 "Longitudine",
- # 5 "Telefono",
- # 6 "Email",
- # 7 "Nome",
- # 8 "Cognome",
- # 9 "Ind-via-num",
- # 10 "Localita",
- # 11 "Provincia",
- # 12 "CAP",
- # 13 "Offerta",
- # 14 "Note"
- # Questi rappresentano un sottoinsieme comune ragionevole
- # intersezione fra i dati di Caterina e quelli che formano
- # una entry dei nodi di accoglienza nella nostra crowdmap
- # I campi letti nel CSV in ingresso bastamortinelmediterraneo
- # sono invece
- # 1 "Nome (Prefisso)"
- # 2 "Nome"
- # 3 "Nome (Medio)"
- # 4 "Cognome"
- # 5 "Nome (Suffisso)"
- # 6 "Telefono"
- # 7 "Email"
- # 8 "Ind-via-num"
- # 9 "Ind-via-num-2"
- # 10 "Localita"
- # 11 "Provincia"
- # 12 "CAP"
- # 13 "Nazione"
- # 14 "Acc-vis-med"
- # 15 "Acc-pra-bur"
- # 16 "Sos-leg"
- # 17 "Sos-psi"
- # 18 "Acc-fam"
- # 19 "Ins-ita"
- # 20 "Lez-scu-gui
- # 21 "Pra-cen"
- # 22 "Osp-eme"
- # 23 "Altro"
- # 24 "Note"
- # 25 "Desidero ricevere il notiziario delle vostre attività"
- # 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)"
- # 27 "Creato da (ID dell'utente)"
- # 28 "ID Registrazione"
- # 29 "Data della registrazione"
- # 30 "URL sorgente"
- # 31 "ID transazione"
- # 32 "Importo del pagamento"
- # 33 "data pagamento"
- # 34 "Stato del pagamento"
- # 35 "ID articolo"
- # 36 "User Agent"
- # 37 "IP utente"
- # Di questi ultimi, i campi 14-22
- # 14 "Acc-vis-med"
- # 15 "Acc-pra-bur"
- # 16 "Sos-leg"
- # 17 "Sos-psi"
- # 18 "Acc-fam"
- # 19 "Ins-ita"
- # 20 "Lez-scu-gui
- # 21 "Pra-cen"
- # 22 "Osp-eme"
- # vengono condensati dallo script nel campo 13 "Offerta"
- # del CVS in uscita.
- # Usato
- # https://realpython.com/python-csv/
- # https://stackoverflow.com/questions/49527005/python-set-a-quotechar-for-csv-dictwriter
- #https://stackoverflow.com/questions/3348460/csv-file-written-with-python-has-blank-lines-between-each-row
- # https://www.thegeekstuff.com/2013/06/python-list/?utm_source=feedly per aggiungere un elemento al principio di una lista
- # La seconda riga deve essere fatta da
- # ,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
- # perché il primo record dopo l'intestazione se lo perde
- import csv
- import random
- with open('data-solidarieta-2.csv') as csv_rfile, open('data-for-ushahidi.csv', mode='w+', newline='') as csv_wrfile:
- csv_reader = csv.DictReader(csv_rfile)
- fieldnames_write = ['Telefono', 'Email', 'Nome', 'Cognome', 'Ind-via-num', 'Localita', 'Provincia', 'CAP', 'Offerta', 'Note']
- separator = ','
- quote = '"'
- csv_writer = csv.DictWriter(csv_wrfile, fieldnames = fieldnames_write, delimiter = separator, quotechar = quote, quoting=csv.QUOTE_NONNUMERIC)
- csv_writer.writeheader()
- line_count = 0
- for row in csv_reader:
- if line_count == 0:
- print(f'Column names are {", ".join(row)}')
- line_count += 1
- else:
- offerta = ''
- if row['Acc-vis-med']:
- offerta = offerta + row['Acc-vis-med'] + ', '
- if row['Acc-pra-bur']:
- offerta = offerta + row['Acc-pra-bur'] + ', '
- if row['Sos-leg']:
- offerta = offerta + row['Sos-leg'] + ', '
- if row['Sos-psi']:
- offerta = offerta + row['Sos-psi'] + ', '
- if row['Acc-fam']:
- offerta = offerta + row['Acc-fam'] + ', '
- if row['Ins-ita']:
- offerta = offerta + row['Ins-ita'] + ', '
- if row['Lez-scu-gui']:
- offerta = offerta + row['Lez-scu-gui'] + ', '
- if row['Pra-cen']:
- offerta = offerta + row['Pra-cen'] + ', '
- if row['Osp-eme']:
- offerta = offerta + row['Osp-eme'] + ', '
- offerta = offerta[:-2]
- 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']})
- line_count += 1
- print(f'Processed {line_count} lines.')
- with open('data-for-ushahidi.csv') as csv_rfile, open('data-for-ushahidi-all.csv', mode='w+', newline='') as csv_wrfile:
- csv_reader = csv.reader(csv_rfile, delimiter=',')
- csv_writer = csv.writer(csv_wrfile, delimiter=',', quotechar='"', quoting=csv.QUOTE_NONNUMERIC)
- line_count = 0
- for row in csv_reader:
- if line_count == 0:
- # Piazza Stazione Firenze
- lat0 = 43.776272983621894
- lon0 = 11.247024534386583
- # Vinci: 43.769053, 10.919953
- # Vaglia: 43.910595, 11.278009
- latmax = 43.910595
- latmin = lat0 - (latmax - lat0)
- lonmin = 10.919953
- lonmax = lon0 + (lon0 - lonmin)
- row.insert(0,"Nome-nodo")
- row.insert(1,"Descrizione")
- row.insert(2,"Latitudine")
- row.insert(3,"Longitudine")
- #row.pop(14)
- csv_writer.writerow(row)
- line_count += 1
- else:
- row.insert(0,"Rete di solidarietà")
- row.insert(1,"Clicca per vedere la scheda completa a fianco")
- row.insert(2,random.uniform(latmin, latmax))
- row.insert(3,random.uniform(lonmin, lonmax))
- #row.pop(14)
- csv_writer.writerow(row)
- line_count += 1
- print(f'Processed {line_count} lines.')
Add Comment
Please, Sign In to add comment