简体   繁体   中英

How do I export JSON data to CSV using python?

I'm building a site that, based on a user's input, sorts through JSON data and prints a schedule for them into an html table. I want to give it the functionality that once the their table is created they can export the data to a CSV/Excel file so we don't have to store their credentials (logins & schedules in a database). Is this possible? If so, how can I do it using python preferably?

This is not the exact answer but rather steps for you to follow in order to get a solution:

1 Read data from json. some_dict = json.loads(json_string)

2 Appropriate code to get the data from dictionary (sort/ conditions etc) and get that data in a 2D array (list)

3 Save that list as csv: https://realpython.com/python-csv/

I'm pretty lazy and like to utilize pandas for things like this. It would be something along the lines of

import pandas as pd    

file = 'data.json'

with open(file) as j:
    json_data = json.load(j)

df = pd.DataFrame.from_dict(j, orient='index')
df.to_csv("data.csv")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM