简体   繁体   中英

convert JSON to TSV using pandas

I would like to convert JSON to TSV but not from a file or to a file.

 messages = { "1":[ { "id":"4d9edf9b-ab9a-45bc-a453-3ea9ecc97818", "message":"m1", "access": "*" }, { "id":"7c4b41e4-3e4f-40ed-9aa4-3854a6b3706a", "message":"m2", "access": "*" }, { "id":"c1c332cf-7cb4-4c5e-bf85-3d7ae3a11625", "message":"m3", "access": "*" }, { "id":"dc9b47b9-d325-475d-9994-a4269faa0873", "message":"m4", "access": "*" } ] } > import sys > import pandas as pd > table = pd.read_json(messages[1], orient='records') > table.to_csv(sys.stdout, sep='\t', index=False)

I tried something like this but it did not work. please help me

To convert the list of Json into DataFrame you should

 table=pd.DataFrame.from_records(messages["1"])

then save as you did in the post.

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