简体   繁体   中英

read json object and create csv string from it in python

read json object and create csv string from it in python.

I have a object array in string format as.

'[{"date":"2014-10-05T01:12:00.000Z","count":56.4691}, {"date":"2014-10-05T01:14:00.000Z","count":23.4691}, ...]'

I want to transform the string into csv format as,

"","date","count"
"1",2014-09-25 14:01:00,182.478
"2",2014-09-25 14:01:00,182.478

to be able to do it, I firstly read the string with read_json function in pandas library. but it sorted the columns. and count column comes before the date column. how can i get this transformation in python?

Use columns parameter in df.to_csv

Ex:

import pandas as pd

s = '[{"date":"2014-10-05T01:12:00.000Z","count":56.4691}, {"date":"2014-10-05T01:14:00.000Z","count":23.4691}]'

df = pd.read_json(s)
df.to_csv(r"PATH\B.csv", columns=["date", "count"])

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