Skip to content

Instantly share code, notes, and snippets.

@shippy
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shippy/26cf39b395dac04ff62f to your computer and use it in GitHub Desktop.
Save shippy/26cf39b395dac04ff62f to your computer and use it in GitHub Desktop.
# The output of the script is meant to be piped to an output csv file.
# Usage:
# python foursquare-parse.py > foursquare.csv
import xml.etree.ElementTree as ET
from dateutil import parser
from datetime import *
tree = ET.parse('foursquare.kml') # or whatever the kml file is called
root = tree.getroot()
print ",".join( ['name', 'href', 'datetime', 'lon', 'lat'] ) # csv header
for place in root.iter('Placemark'):
name = place[0].text
href = place[1][0].attrib['href']
datetime = place[2].text
datetime = (parser.parse(datetime) - timedelta(hours = 5)).strftime("%Y-%m-%d %H:%M:%S") # adjusting for timezone and converting to CartoDB-acceptable format
coords = place[5][2].text.split(",")
print '"' + '","'.join( [name, href, datetime, coords[0], coords[1]] ) + '"'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment