Skip to content

Instantly share code, notes, and snippets.

@bauripalash bauripalash/json2csv.py

Last active Apr 9, 2020
Embed
What would you like to do?
import json
import pandas as pd
import sys
import requests
COLS = {'agebracket': 'AGE', 'backupnotes': 'BACKUP NOTES', 'contractedfromwhichpatientsuspected': 'SUSPECTED INFECTION FROM PATIENT', 'currentstatus': 'STATUS', 'dateannounced': 'ANNOUNCED DATE', 'detectedcity': 'DETECTED CITY', 'detecteddistrict': 'DETECTED DISTRICT', 'detectedstate': 'DETECTED STATE', 'estimatedonsetdate': 'ESTIMATED ONSET DATE', 'gender': 'GENDER', 'nationality': 'NATIONALITY', 'notes': 'NOTES', 'patientnumber': 'PATIENT NUMBER', 'source1': 'SOURCE 1', 'source2': 'SOURCE 2', 'source3': 'SOURCE 3', 'statecode': 'STATE CODE', 'statepatientnumber': 'STATE PATIENT NUMBER', 'statuschangedate': 'STATUS CHANGE DATE', 'typeoftransmission': 'TYPE OF TRANSMISSION'}
RCOLS = ["PATIENT NUMBER" , "AGE" , "GENDER" , "STATUS", "ANNOUNCED DATE" , "TYPE OF TRANSMISSION" ,"DETECTED CITY" , "DETECTED DISTRICT" , "DETECTED STATE" , "STATE CODE" , "NATIONALITY" , "STATE PATIENT NUMBER" , "STATUS CHANGE DATE" , "SUSPECTED INFECTION FROM PATIENT" , "ESTIMATED ONSET DATE" , "SOURCE 1" , "SOURCE 2" , "SOURCE 3" , "NOTES" , "BACKUP NOTES"]
def main():
j = "https://raw.githubusercontent.com/covid19india/api/master/raw_data.json"
c = "data.csv"
try:
res = requests.get(j).json()["raw_data"]
df = pd.read_json(json.dumps(res))
df.rename(COLS , axis = 'columns' , inplace=True)
df = df.reindex(columns=RCOLS)
df.to_csv(c , index=False)
print("DONE!")
except Exception as e:
print(e)
print("FAILED")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.