我有一个JSON文件,我想转换为CSV。我这样做:
import pandas as pd
with open('test.json', encoding='utf-8') as inputfile:
df = pd.read_json(inputfile)
df.to_csv('test.csv', encoding='utf-8', index=False)
字符串
一切正常,但我的JSON有结构,我想“清理”,使转换更好。我的结构是:
"field A": 117,
"field B": 143,
"field C": 27,
"field D": [
{
"id": 782,
"name": "Some test A",
"type": "Group"
}
],
"field E": null,
"field F": "contact",
"field G": [
{
"id": 32358,
"name": "Some test B",
"type": "Note"
},
{
"id": 37557,
"name": "Some test C",
"type": "Note"
},
{
"id": 38416,
"name": "Some test D",
"type": "Note"
}
],
"field H": null,
型
我想删除所有的“id”和“type”,只留下“name”,如果有多个名字exsists有他们在新的一行。
"field A": 117,
"field B": 143,
"field C": 27,
"field D": "Some test A",
"field E": null,
"field F": "contact",
"field G": "Some test B \n Some test C \n Some test D",
"field H": null,
型
有什么建议吗?谢谢!
2条答案
按热度按时间acruukt91#
你需要通过键/值来重新定义,从
list
创建新值,然后用新值更新dict:字符串
输出量:
型
yzckvree2#
一个非常特定于结构的代码可以是:
字符串