0

so I have a TSV file that contains the location of parks and I'm trying to add it to a base GoogleMaps API address to eventually write a GeoJSON file.

here's what it the issue is.. I can't get the formatting down so that the address base I have is concatenated to the base GoogleMaps API url. The basic code is this:

def geocode(address):
    url = ("http://maps.googleapis.com/maps/api/geocode/json?"
        "sensor=false&address={0}".format(address.replace(" ", "+")))
    print url

with open("MovieParksFixed.tsv", "rU") as f:
    reader = csv.DictReader(f, delimiter = "\t")
    for line in reader:
        response = geocode(line['Location'])

but running this outputs:

http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=
Edgebrook+Park,+Chicago+
http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=
Gage+Park,+Chicago+

and so on , where the first line just won't connect to the second line. So what I end up with is http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address= and then Edgebrook+Park,+Chicago+on the following line, but not connected.

I swear it's like there's a hidden newline or something that's screwing it up...

I had to manually edit the one of two cells of the parsed TSV file a bit on Excel (but still looks fine now - https://github.com/yongcho822/Movies-in-the-park/blob/master/MovieParksFixed.tsv)... did that screw it all up or something?

note: the original TSV file when written was obviously delimited by tabs...

SpicyClubSauce
  • 4,076
  • 13
  • 37
  • 62

1 Answers1

2

Before you interpolate your string into the URL string, try the following:

address.strip().replace(" ", "+")

The strip() method will remove all leading and trailing whitespace (tabs, newlines, spaces etc.). The final line:

url = ("http://maps.googleapis.com/maps/api/geocode/json?"
    "sensor=false&address={0}".format(address.strip().replace(" ", "+")))
Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • halle friggin luyah good sir. Maybe because I had opened it in excel to make changes, ipython is giving me this error after running the whole geocode error (the files when originally written to the TSV file were explicitly encoded in UTF-8)- MovieParksGeocode.tsv is not UTF-8 encoded. Saving disabled. Any suggestions? – SpicyClubSauce May 22 '15 at 05:49
  • Did you save the file after opening it in Excel? Not very familiar with Excel, but if so, you might find your solution here http://stackoverflow.com/questions/4221176/excel-to-csv-with-utf8-encoding – Martin Konecny May 22 '15 at 06:16