comela.blogg.se

Csv to json in python
Csv to json in python













csv to json in python

On the Right Hand Side, we break up the 'stats' column using apply to make a data frame out of each key/value pair. Each element in the stats column is a dictionary. Iterate over each row and update a newly-created dictionary myjson using one of the column values as key: myjson key row Store the myjson dictionary data in a JSON file using the json.dumps (myjson) function. CSV and JSON are both good ways to organize text-based data, suitable for sharing between applications, or between browser and server. On the Left Hand Side, we get the new column names from the keys of the element of the stats column. Load the CSV content into Python using the csv.DictReader (fobj) and pass the file object just created. This will only work for regular data (the json object needs to have 3 values or at least missing values need to be handled in our CustomParser) df.keys())] = df.apply(pandas.Series) This will make the stats column a dictįrom here, we can use a little hack to directly append these columns in one step with the appropriate column names. We are telling read_csv to read the data in the standard way, but for the stats column use our custom parsers. I am working with CSV files where several of the columns have a simple json object (several key value pairs) while other columns are normal.















Csv to json in python