I've written a small script that scans the content of a csv file for a series of location names, and then uses Geolocation API to fetch and display Latitue and Longitude co-ordinates.
Here, I've assumed that always the second column of the csv contains the location name. A variable realname corresponds to that column in para1,realname=zip(*data)
. But now I want to make the program more general and robust, and ask the user to enter a custom column number to read location names from. How can I read data from a user specified column from a CSV in python?
import csv
import urllib
import xml.etree.ElementTree as ET
path=raw_input("Enter source filepath: ")
# argnum=int(raw_input("Parameter column No: "))
latlist=list()
lnglist=list()
try:
with open(path,'r+') as filein:
data=csv.reader(filein, skipinitialspace = True)
para1,realname=zip(*data)
for item in realname:
urlpath="http://maps.googleapis.com/maps/api/geocode/xml?address="+ item + "&sensor=true"
xml = urllib.urlopen(urlpath)
tree = ET.parse(xml)
root = tree.getroot()
for location in root.iter('location'):
lat=location.find('lat').text
latlist.append(lat)
lng=location.find('lng').text
lnglist.append(lng)
filein.close()
print "\n\nLATS\n==========================\n"
for lats in latlist:
print lats
print "\n\nLONGS\n==========================\n"
for longs in lnglist:
print longs
except Exception,e:
print str(e)
One way would be to use a generator expression to extract the desired column from each row of data read from the csv file, and then iterate over that with afor
loop.
A nice thing about this approach is that it could easily be expanded to extract multiple columns from each row if needed. It's also fairly memory efficient because it iteratively processes one row at a time from the file.
import csv
import urllib
import xml.etree.ElementTree as ET
path = raw_input("Enter source filepath: ")
col = int(raw_input("Column # to extract (starting with zero): "))
latlist = []
lnglist = []
try:
with open(path, 'r+') as filein:
data = csv.reader(filein, skipinitialspace=True)
for item in (row[col] for row in data):
urlpath = (
"http://maps.googleapis.com/maps/api/geocode/xml?address=" +
item + "&sensor=true")
xml = urllib.urlopen(urlpath)
tree = ET.parse(xml)
root = tree.getroot()
for location in root.iter('location'):
lat = location.find('lat').text
latlist.append(lat)
lng = location.find('lng').text
lnglist.append(lng)
print "\n\nLATS\n==========================\n"
for lats in latlist:
print lats
print "\n\nLONGS\n==========================\n"
for longs in lnglist:
print longs
except Exception as e:
print str(e)
P.S. Since your exception handler does nothing but print the exception, it might be better to just leave thetry/except
block out because that's what would happen automatically anyway, plus it would print something very useful called a traceback which will show exactly where the problem occurred.