Python:从json/字典创建csv



我有以下数据需要转换为csv:

"tfeOrganizations": [
{
"org": "MyOrg1",
"org_admins": [
"foo@bar.com", "foo2@bar2.com"
],
"org_viewers": [
"foo3@bar3.com","foo4@bar4.com"
],
"teams": {
"genericresources-admins": {
"ci_cd_token_regenerate": False,
"members": ["firstUser@test.com", "secondUser@test.com"],
"workspaces": [
[
"genericresources-dev",
"write"
],
[
"genericresources-qa",
"write"
],
[
"genericresources-prod",
"write"
]
]
},
"genericresources-contributors": {
"ci_cd_token_regenerate": False,
"members": ["thirdUser@test.com", "forthUser@test.com", "fithUser@test.com"],
"workspaces": [
[
"genericresources-dev",
"write"
],
[
"genericresources-qa",
"write"
],
[
"genericresources-prod",
"plan"
]
]
}
},
"workspaces": [
"genericresources-dev",
"genericresources-qa",
"genericresources-prod"
]
}
]

结果应该是这样的,而头是基于所有组织中成员的最长数组动态构建的。

organisation,workspace,org_admin1,org_admin2,org_viewer1,org_viewer2,member1,member2,member3,...
MyOrg1,genericresources-dev,foo@bar.com,foo2@bar2.com,foo3@bar3.com,foo4@bar4.com,firstUser@test,sendUser@test.com,...
MyOrg1,genericresources-qa,foo@bar.com,foo2@bar2.com,foo3@bar3.com,foo4@bar4.com,firstUser@test,sendUser@test.com,...
MyOrg1,genericresources-prod,foo@bar.com,foo2@bar2.com,foo3@bar3.com,foo4@bar4.com,firstUser@test,sendUser@test.com,...

目前我不知道如何解决这个问题。

您可以使用pandas 创建

示例

import pandas as pd
# access your json file
your_json = data['tfeOrganizations']

data_frame = pd.DataFrame(your_json)

data_frame.to_csv('result.csv',data_frame)

相关内容

  • 没有找到相关文章

最新更新