Marks Blog
  • PowerDNS Admin
  • About
  • Simple Ticket System

My first Python script

January 20, 2011 by mark

I have always avoided Python. I am not sure why, but I think it is mostly because of the syntax, with the needed indention. Well, today I wrote my first script, and I actually like the language a little.

I needed to lookup GPS coordinates for a long list of addresses, and didn’t like doing it manually. Using Google’s maps API, I solved the problem with this little script. It reads the addresses from stdin and writes addresss;latitude;longtitude to stdout.

#!/usr/bin/env python

# Importing modules
import urllib
import json
import sys

# Configuration of main URL
URL = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='

# Read addresses from stdin
for address in sys.stdin:
    #print "Looking up: " + str(address)

    # URL Encode the address
    encoded_address = urllib.quote(address.strip())
    real_url = URL + encoded_address

    # Reset buffer
    buffer = ''

    # Fetch the json result
    try:
        fh = urllib.urlopen(real_url)
        for line in fh:
            buffer += line
        fh.close
    except Exception:
        print "Ops, caught an exception."
        sys.exit(1)
    
    # Try to de-serialize the json string
    result = json.loads(buffer)

    # Check status of result
    if result['status'] == "OK":
        #print "Results looks good, continue"
        for r in result['results']:
            lat = str(r['geometry']['location']['lat'])
            lng = str(r['geometry']['location']['lng'])
            print '%s; lat=%s; lng=%s' % (address.strip(), lat, lng)

# Finish

Posted in: Development & Scripting Tagged: gps, maps, python

Weather Station

Pages

  • About
  • PowerDNS Admin
  • Simple Ticket System

Affiliates

  • Danish Sensor Engineering
  • Mintr ApS
  • Nellemann Data

Social

  • BitBucket
  • GitHub
  • LinkedIn
  • Mastodon

Categories

  • Android
  • Automobile
  • Blog & Rantings
  • Development & Scripting
  • Grails
  • Linux & Unix
  • Mac OS X
  • Misc
  • No category
  • PowerDNS Admin
  • Puppet
  • Security Related
  • VirtualBox
  • Virtualization
  • VMware
  • Windows

Archives

  • January 2019
  • September 2017
  • January 2016
  • May 2015
  • March 2015
  • February 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • June 2014
  • January 2014
  • July 2013
  • June 2013
  • February 2013
  • January 2013
  • December 2012
  • August 2012
  • May 2012
  • October 2011
  • April 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • March 2010
  • October 2009
  • September 2009
  • April 2009
  • January 2009
  • March 2008
  • January 2008
  • November 2007
  • January 2007
  • December 2006
  • November 2006
  • August 2006
  • June 2006
  • March 2006
  • October 2005
  • August 2005

Copyright © 2025 Marks Blog.

Mobile WordPress Theme by themehall.com