简体   繁体   English

使用 pandas 将 JSON 转换为 TSV

[英]convert JSON to TSV using pandas

I would like to convert JSON to TSV but not from a file or to a file.我想将 JSON 转换为 TSV 但不是从文件或文件。

 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要将 Json 的列表转换为 DataFrame,您应该

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

then save as you did in the post.然后像在帖子中一样保存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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