我正在尝试从要爬网的数据创建结构化JSON对象。
我抓取的数据存储在几个列表中。 list1
包含事件名称,list2
包含事件类别,list3
包含原始事件数据(以与事件类别相同的顺序存储)
list1 = [['event1'], ['event2'], ['event3']]
list2 = [['team' , 'score', 'date'], ['team', 'location', 'date'], ['team', ' record']]
list3 = [['team1' , 'score1', 'date1', 'team2' , 'score2', 'date2'],
['team1' , 'location1', 'date1', 'team2' , 'location2', 'date2'],
['team1', 'record 1', 'team2', 'record2', 'team3', 'record3']]
我正在尝试创建以下格式的json文件:
{
"event1": {
"1": {
"team": team1,
"score": score1,
"date": date1,
},
"2": {
"team": team2,
"score": score2,
"date": date2,
},
},
"event2": {
"1": {
"team": team1,
"location": location1,
"date": date1,
},
"2": {
"team": team2,
"location": location2,
"date": date2,
},
"event3": {
"1": {
"team": team1,
"record": record1,
},
"2": {
"team": team2,
"record": record2,
},
"3": {
"team": team3,
"record": record3,
},
}
是否可以在python上以这种方式进行结构化?我可以使用其他语言的for循环来做到这一点,但是我在如何用python附加数据方面颇费心思。
答案 0 :(得分:0)
希望我能理解我的解决方案。
list1 = [['event1'], ['event2'], ['event3']]
list2 = [['team' , 'score', 'date'], ['team', 'location', 'date'], ['team', ' record']]
list3 = [['team1' , 'score1', 'date1', 'team2' , 'score2', 'date2'], ['team1' , 'location1', 'date1', 'team2' , 'location2', 'date2'], ['team1', 'record 1', 'team2', 'record2', 'team3', 'record3']]
out = {}
#iterate over each of the lists together
for (a,), b, c in zip(list1, list2, list3):
#initialise the "event." attribute in the `out` dict
out[a] = {}
#chunk the lists in `list3` to the size of the lists in `list2` and iterate
for i, l in enumerate([c[i:i+len(b)] for i in range(0, len(c), len(b))]):
#for this chunk, initiate a sub dictionary under the "event." attr in `out`
out[a][str(i+1)] = {}
#iterate over the elements in the list from `list2`
for j, e in enumerate(b):
#assign the corresponding chunk element to the appropriate location.
out[a][str(i+1)][e] = l[j]
给出:
{
"event1": {
"1": {
"team": "team1",
"score": "score1",
"date": "date1"
},
"2": {
"team": "team2",
"score": "score2",
"date": "date2"
}
},
"event2": {
"1": {
"team": "team1",
"location": "location1",
"date": "date1"
},
"2": {
"team": "team2",
"location": "location2",
"date": "date2"
}
},
"event3": {
"1": {
"team": "team1",
" record": "record 1"
},
"2": {
"team": "team2",
" record": "record2"
},
"3": {
"team": "team3",
" record": "record3"
}
}
}