Skip to content

Instantly share code, notes, and snippets.

@StevenMaude
Last active May 19, 2026 23:23
Show Gist options
  • Select an option

  • Save StevenMaude/883fdd4db9f5a8688d11 to your computer and use it in GitHub Desktop.

Select an option

Save StevenMaude/883fdd4db9f5a8688d11 to your computer and use it in GitHub Desktop.
Quick hacky script to extract date and 2D length data from GPX files to CSV using gpxpy; at least compatible with Py2.7/3.4
#!/usr/bin/env python
""" Extract date and 2D length data from GPX files in current directory. """
from __future__ import division, print_function
import decimal
import glob
import gpxpy
def main():
gpx_filenames = glob.glob("*.gpx")
print("Filename,Date (YYYY-MM-DD),Time,"
"Length (km),ISO year,ISO week number")
for gpx_filename in gpx_filenames:
with open(gpx_filename, 'r') as f:
gpx = gpxpy.parse(f.read())
for track in gpx.tracks:
extract_track_data(track, gpx_filename)
def extract_track_data(track, gpx_filename):
""" Take GPX track and string filename; print comma separated data. """
start_datetime, _ = track.get_time_bounds()
iso_year, iso_week_number, _ = start_datetime.isocalendar()
km_2d_length = decimal.Decimal(track.length_2d()/1000)
output = ','.join([gpx_filename,
str(start_datetime.date()),
str(start_datetime.time()),
str(round(km_2d_length, 2)),
str(iso_year),
str(iso_week_number)])
print(output)
if __name__ == '__main__':
main()

Comments are disabled for this gist.