Skip to content

Instantly share code, notes, and snippets.

@stucka
Last active March 24, 2019 12:14
Show Gist options
  • Save stucka/3ea80ae3bb56daf07fcd570b3a227132 to your computer and use it in GitHub Desktop.
Save stucka/3ea80ae3bb56daf07fcd570b3a227132 to your computer and use it in GitHub Desktop.
Turn NTSB airplane crash database "CSV" into usable airplane accident / crash data with actual working CSV
import requests
import csv
import re
hosturl = "http://app.ntsb.gov/aviationquery/Download.ashx?type=csv"
targetraw = "ntsbdb.raw"
targetcsv = "ntsbdb.csv"
r = requests.get(hosturl)
with open(targetraw, "wb") as f:
f.write(r.content)
with open(targetraw, "r") as f:
core = f.read().splitlines()
with open(targetcsv, "w", newline="") as f:
writer = csv.writer(f)
for row in core:
row = re.sub(r" \| $", "", row)
row = row.split(" | ")
writer.writerow(row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment