Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Convert Locations.kml (pulled from an iPhone backup) to SQLite
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="">
<name>2015-12-18 19:12:32 Source: WhatsApp</name>
<name>2018-04-20 22:33:36 Source: WhatsApp</name>
<name>2015-02-02 20:32:46 Source: Photos</name>
from xml.etree import ElementTree as ET
import sys
import sqlite_utils
KML = "{}"
def iterate_kml(filepath):
fp = open(filepath)
parser = ET.XMLPullParser(["end"])
while True:
chunk = * 8)
for event, element in parser.read_events():
assert event == "end"
if element.tag == f"{KML}Placemark":
datetime = element.find(f".//{KML}when").text
source = element.find(f".//{KML}name").text.split("Source: ")[-1]
longitude, latitude = map(
float, element.find(f".//{KML}coordinates").text.split(",")
yield {
"datetime": datetime,
"source": source,
"latitude": latitude,
"longitude": longitude,
if not chunk:
if __name__ == "__main__":
db = sqlite_utils.Database("phone-locations.db")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment