Skip to content

Instantly share code, notes, and snippets.

@tonypiazza
Last active March 30, 2020 22:12
Show Gist options
  • Save tonypiazza/7a1d1006566cd4962f14f9830d833611 to your computer and use it in GitHub Desktop.
Save tonypiazza/7a1d1006566cd4962f14f9830d833611 to your computer and use it in GitHub Desktop.
Reshape the JHU data files
import csv
import sys
import traceback
FIELD_NAMES = ['Province_State', 'Country_Region',
'Lat', 'Long', 'Date', 'Count']
if __name__ == '__main__':
count = 0
try:
with open(sys.argv[1], 'r') as input_file:
reader = csv.DictReader(input_file)
with open(sys.argv[2], 'w') as output_file:
writer = csv.DictWriter(
output_file, FIELD_NAMES, extrasaction='ignore', quoting=csv.QUOTE_MINIMAL)
writer.writeheader()
for row_in in reader:
row_out = dict()
row_out['Province_State'] = row_in['Province/State']
row_out['Country_Region'] = row_in['Country/Region']
row_out['Lat'] = row_in['Lat']
row_out['Long'] = row_in['Long']
for key in reader.fieldnames[4:]:
row_out['Date'] = key
row_out['Count'] = row_in[key]
writer.writerow(row_out)
count += 1
except Exception:
traceback.print_exc()
if count:
print(f'Generated {count} rows')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment