Last active
March 24, 2019 12:14
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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