First of all let me say I am completly new in Python, and have very little experience in programming. I decided to use Python as an environment for some job automation at my work. I need to sort some amount of date exported to XLS, geocode addresses and then create heatmap/population map on google API maps or OpenSourceMaps. SOme solution I already have found but having problem to combine them, and stuck at very erly stage.
please see my code:
import os
import pandas
import geopy
import urllib
import csv
import numpy
import geopy
# - - - - - - - - -
fin = open ("source_file.csv","r") # open input file for reading
users_dict = {}
with open('output_file.csv', 'wb') as fout: # output csv file
writer = csv.writer(fout)
with open('source_file.csv','r') as csvfile: # input csv file
reader = csv.DictReader(csvfile, delimiter=',')
for row in reader:
location_string = row['city'] + ',' + row['street'] + ' ' + row['house']
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode(location_string)
row['lat']=latitude = location.latitude
row['lng'] = location.longitude
users_dict.update(row)
print (users_dict)
fout.close()
fin.close()
input file format
unit_no,kod,street,house,city,lng,lat
123456,00-001,nowy swiat,4,warszawa,,
dictionary output:
{'unit_no': '123456', 'kod': '00-001', 'street': 'nowy swiat', 'house': '4', 'city': 'warszawa', 'lng': 21.0220598, 'lat': 52.2302825}
result: output_file.csv is zero size output_file.csv opened shows nothing in
expected output file with data:
unit_no,kod,street,house,city,lng,lat
123456,00-001,nowy swiat,4,warszawa,21.0220598,52.2302825
In my mind next stage is to create geojson file from created output and after that visualize it my markers or population on the map.
Thank you in advance for valuable advices.
Make a header list and write it first in output_file.csv
header = ['unit_no','kod','street','house','city','lng','lat']
writer.writerow(header)
This will make the header first row of csv and then the output dictionary will relate with first row of csv each time and add the data correspondingly.
if I understood you well my code should look like this:
with open('output_file.csv', 'wb') as fout: # output csv file
writer = csv.writer(fout)
header = ['unit_no','kod','street','house','city','lng','lat'] # your suggestion
writer.writerow(header) # your suggestion
with open('source_file.csv','r') as csvfile: # input csv file
and finished with:
print (users_dict)
writer = csv.DictWriter(fout, delimiter=',') # your suggestion
writer.writerows(user_dict) # your suggestion
fout.close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.