将 JSON 转换为 CSV 并使用 Python 合并对象
convert JSON into CSV and merge object using Python
我有 JSON 文件,需要转换成 CSV 格式。
示例 JSON 数据:
{
"books":[
{
"id":"1",
"story":{
"title":"Lonely lion",
"writers":[
{
"lastName":"John",
"firstName":"Louis"
},
{
"lastName":"Nick",
"firstName":"Fed"
}
],
"slot":[
{
"slotid":1112,
"region":{
"US":11,
"CA":1
}
}
]
},
"description":[
{
"release":false,
"author":[
{
"name":"John",
"main":1
},
{
"name":"Jeroge",
"main":0
},
{
"name":"Peter",
"main":0
}
]
}
]
},
{
"id":"2",
"story":{
"title":"Lonely lion",
"writers":[
{
"lastName":"John",
"firstName":"Louis"
},
{
"lastName":"Nick",
"firstName":"Fed"
}
],
"slot":[
{
"slotid":1112,
"region":{
"US":11
}
}
]
},
"description":[
{
"release":false,
"author":[
{
"name":"Jeroge",
"main":1
}
]
}
]
}
]
}
我使用了下面的代码:
with open(jsonFilename, 'r',encoding='UTF-8') as jsonInput:
json_data = json.loads(jsonInput.read())
cols=['id','title','authorname','authortmain','writers','slotinfo']
mergeList = []
for i,books in enumerate(json_data["books"]):
for j in range(len(books ["description"][0]['author'])):
id=books['id']
title=books["story"]['title']
authorname=books["description"][0]['author'][j]['name']
authortmain=books["description"][0]['author'][j]['main']
writers=books["story"]['writers']
slotinfo=books["story"]['slot']
mergeList.append((id,title,authorname,authortmain,writers,slotinfo))
它给了我以下输出:作者和插槽信息以列表形式出现,但我需要使用 spearator 将它们合并。
我得到的输出:
id,title,authorname,authortmain,writers,slotinfo
1,Lonely lion,John,1,"[{'lastName': 'John', 'firstName': 'Louis'}, {'lastName': 'Nick', 'firstName': 'Fed'}]","[{'slotid': 1112, 'region': {'US': 11, 'CA': 1}}]"
1,Lonely lion,Jeroge,0,"[{'lastName': 'John', 'firstName': 'Louis'}, {'lastName': 'Nick', 'firstName': 'Fed'}]","[{'slotid': 1112, 'region': {'US': 11, 'CA': 1}}]"
1,Lonely lion,Peter,0,"[{'lastName': 'John', 'firstName': 'Louis'}, {'lastName': 'Nick', 'firstName': 'Fed'}]","[{'slotid': 1112, 'region': {'US': 11, 'CA': 1}}]"
2,Lonely lion,Jeroge,1,"[{'lastName': 'John', 'firstName': 'Louis'}, {'lastName': 'Nick', 'firstName': 'Fed'}]","[{'slotid': 1112, 'region': {'US': 11, 'CA': 1}}]"
但我想使用“/”作为分隔符来合并作者信息和插槽信息。
我需要 运行 其他循环,但我有点困惑如何实现它。
期望的输出:
1,Lonely lion,John,1,"John,Louis/Nick,Fed","US: 11/CA: 1"
1,Lonely lion,Jeroge,0,"John,Louis/Nick,Fed","US: 11/CA: 1"
1,Lonely lion,Peter,0,"John,Louis/Nick,Fed","US: 11/CA: 1"
2,Lonely lion,Jeroge,1,"John,Louis/Nick,Fed","US: 11"
它适用于 python3.x
。在 python2.7
中,encoding='UTF-8'
将不起作用。
import json
with open(jsonFilename, 'r', encoding='UTF-8') as jsonInput:
json_data = json.loads(jsonInput.read())
cols=['id','title','authorname','authortmain','writers','slotinfo']
mergeList = []
for i,books in enumerate(json_data["books"]):
for j in range(len(books ["description"][0]['author'])):
id=books['id']
title=books["story"]['title']
authorname=books["description"][0]['author'][j]['name']
authortmain=books["description"][0]['author'][j]['main']
# writers=books["story"]['writers']
writers = []
for writer in books['story']['writers']:
name = '{},{}'.format(writer['firstName'], writer['lastName'])
writers.append(name)
# slotinfo=books["story"]['slot']
writers = '/'.join(writers)
slotinfo = []
for slot in books['story']['slot']:
for key,value in slot['region'].items():
item = '{}: {}'.format(key, value)
slotinfo.append(item)
slotinfo = '/'.join(slotinfo)
mergeList.append((id,title,authorname,authortmain,writers,slotinfo))
print(mergeList)
我有 JSON 文件,需要转换成 CSV 格式。 示例 JSON 数据:
{
"books":[
{
"id":"1",
"story":{
"title":"Lonely lion",
"writers":[
{
"lastName":"John",
"firstName":"Louis"
},
{
"lastName":"Nick",
"firstName":"Fed"
}
],
"slot":[
{
"slotid":1112,
"region":{
"US":11,
"CA":1
}
}
]
},
"description":[
{
"release":false,
"author":[
{
"name":"John",
"main":1
},
{
"name":"Jeroge",
"main":0
},
{
"name":"Peter",
"main":0
}
]
}
]
},
{
"id":"2",
"story":{
"title":"Lonely lion",
"writers":[
{
"lastName":"John",
"firstName":"Louis"
},
{
"lastName":"Nick",
"firstName":"Fed"
}
],
"slot":[
{
"slotid":1112,
"region":{
"US":11
}
}
]
},
"description":[
{
"release":false,
"author":[
{
"name":"Jeroge",
"main":1
}
]
}
]
}
]
}
我使用了下面的代码:
with open(jsonFilename, 'r',encoding='UTF-8') as jsonInput:
json_data = json.loads(jsonInput.read())
cols=['id','title','authorname','authortmain','writers','slotinfo']
mergeList = []
for i,books in enumerate(json_data["books"]):
for j in range(len(books ["description"][0]['author'])):
id=books['id']
title=books["story"]['title']
authorname=books["description"][0]['author'][j]['name']
authortmain=books["description"][0]['author'][j]['main']
writers=books["story"]['writers']
slotinfo=books["story"]['slot']
mergeList.append((id,title,authorname,authortmain,writers,slotinfo))
它给了我以下输出:作者和插槽信息以列表形式出现,但我需要使用 spearator 将它们合并。
我得到的输出:
id,title,authorname,authortmain,writers,slotinfo
1,Lonely lion,John,1,"[{'lastName': 'John', 'firstName': 'Louis'}, {'lastName': 'Nick', 'firstName': 'Fed'}]","[{'slotid': 1112, 'region': {'US': 11, 'CA': 1}}]"
1,Lonely lion,Jeroge,0,"[{'lastName': 'John', 'firstName': 'Louis'}, {'lastName': 'Nick', 'firstName': 'Fed'}]","[{'slotid': 1112, 'region': {'US': 11, 'CA': 1}}]"
1,Lonely lion,Peter,0,"[{'lastName': 'John', 'firstName': 'Louis'}, {'lastName': 'Nick', 'firstName': 'Fed'}]","[{'slotid': 1112, 'region': {'US': 11, 'CA': 1}}]"
2,Lonely lion,Jeroge,1,"[{'lastName': 'John', 'firstName': 'Louis'}, {'lastName': 'Nick', 'firstName': 'Fed'}]","[{'slotid': 1112, 'region': {'US': 11, 'CA': 1}}]"
但我想使用“/”作为分隔符来合并作者信息和插槽信息。 我需要 运行 其他循环,但我有点困惑如何实现它。
期望的输出:
1,Lonely lion,John,1,"John,Louis/Nick,Fed","US: 11/CA: 1"
1,Lonely lion,Jeroge,0,"John,Louis/Nick,Fed","US: 11/CA: 1"
1,Lonely lion,Peter,0,"John,Louis/Nick,Fed","US: 11/CA: 1"
2,Lonely lion,Jeroge,1,"John,Louis/Nick,Fed","US: 11"
它适用于 python3.x
。在 python2.7
中,encoding='UTF-8'
将不起作用。
import json
with open(jsonFilename, 'r', encoding='UTF-8') as jsonInput:
json_data = json.loads(jsonInput.read())
cols=['id','title','authorname','authortmain','writers','slotinfo']
mergeList = []
for i,books in enumerate(json_data["books"]):
for j in range(len(books ["description"][0]['author'])):
id=books['id']
title=books["story"]['title']
authorname=books["description"][0]['author'][j]['name']
authortmain=books["description"][0]['author'][j]['main']
# writers=books["story"]['writers']
writers = []
for writer in books['story']['writers']:
name = '{},{}'.format(writer['firstName'], writer['lastName'])
writers.append(name)
# slotinfo=books["story"]['slot']
writers = '/'.join(writers)
slotinfo = []
for slot in books['story']['slot']:
for key,value in slot['region'].items():
item = '{}: {}'.format(key, value)
slotinfo.append(item)
slotinfo = '/'.join(slotinfo)
mergeList.append((id,title,authorname,authortmain,writers,slotinfo))
print(mergeList)