我正在尝试解析某些html表内容,并且有一个如下所示的有效负载:
"payload": [
{
"Cell #1": "Origin",
"Cell #2": "Destination",
"Cell #3": "Miles",
"Cell #4": "Rate"
},
{
"Cell #1": "Pampa, TX",
"Cell #2": "Sallisaw, OK",
"Cell #3": "207",
"Cell #4": "$725"
},
{
"Cell #1": "Pampa, TX",
"Cell #2": "Sallisaw, OK",
"Cell #3": "207",
"Cell #4": "$725"
},
{
"Cell #1": "Pampa, TX",
"Cell #2": "Atoka, OK",
"Cell #3": "176",
"Cell #4": "$625"
},
{
"Cell #1": "Pampa, TX",
"Cell #2": "Wichita, KS",
"Cell #3": "460",
"Cell #4": "$1,150"
}
]
您可以看到此列表中的第一个字典是表标题。然后,后面的每个项目都按其相应的标题排序。
我正在尝试使其看起来像这样:
[
{
"Origin": "Pampa, TX",
"Destination": "Sallisaw, OK",
"Miles": "207",
"Rate": "$725"
},
]
要注意的是,第一个列表项(即标题)可以更改为添加/删除“列”,因此它必须具有通用性。
我在尝试什么 首先,我认为每个字典都应转换为它们的值列表:
rows = []
for row in rows:
rows.append(row.values)
这将输出列表列表:
rows = [['Origin', 'Destination', 'Miles', 'Rate']), ['Pampa, TX', 'Sallisaw, OK', '207', '$725'], ['Pampa, TX', 'Sallisaw, OK', '207', '$725'], ['Pampa, TX', 'Atoka, OK', '176', '$625'], ['Pampa, TX', 'Wichita, KS', '460', '$1,150']]
通常,如果我要进行转换,并且在接收有效负载之前就知道了标头,那么我将能够基于预设的标头构建字典。
类似的东西:
for r in rows:
converted_row = {
"Origin": r[0],
"Destination": r[1],
"Miles": r[2],
"Rate": r[3]
}
但这并不能解决动态列的问题。还假设顺序始终匹配。我觉得列表理解有一种更动态的方法。
答案 0 :(得分:4)
如果dicts
是您的词典列表,并且您至少使用Python 3.7以便我们可以假定dict项的确定顺序,则可以发出:
>>> keys = dicts[0].values()
>>> [dict(zip(keys, d.values())) for d in dicts[1:]]
[{'Origin': 'Pampa, TX', 'Destination': 'Sallisaw, OK', 'Miles': '207', 'Rate': '$725'},
{'Origin': 'Pampa, TX', 'Destination': 'Sallisaw, OK', 'Miles': '207', 'Rate': '$725'},
{'Origin': 'Pampa, TX', 'Destination': 'Atoka, OK', 'Miles': '176', 'Rate': '$625'},
{'Origin': 'Pampa, TX', 'Destination': 'Wichita, KS', 'Miles': '460', 'Rate': '$1,150'}]
答案 1 :(得分:4)
假设您将此列表存储在名为payload
的变量中...
您只需从第一个条目中获取标头,然后将其用作其余条目中的值的键,然后使用列表理解将它们转换为词典列表
基于OP,我假设您想要所有条目的类似词典的列表:
headers = payload[0].values()
output = [dict(zip(headers,entry.values())) for entry in payload[1:]]
输出:
[{'Destination': 'Sallisaw, OK',
'Miles': '207',
'Origin': 'Pampa, TX',
'Rate': '$725'},
{'Destination': 'Sallisaw, OK',
'Miles': '207',
'Origin': 'Pampa, TX',
'Rate': '$725'},
{'Destination': 'Atoka, OK',
'Miles': '176',
'Origin': 'Pampa, TX',
'Rate': '$625'},
{'Destination': 'Wichita, KS',
'Miles': '460',
'Origin': 'Pampa, TX',
'Rate': '$1,150'}]
答案 2 :(得分:0)
您可以通过非常容易理解的代码来使用它
#Declare an empty list to record the desired keys
keys=[]
#Filling the keys (dynamic and can be used for any other object)
for i in p[0].values():
keys.append(i)
#Declare an empty ans list
ans=[]
#Loop over the given payload values from 2nd value to last
for i in p[1:]:
#making the current ith dict values as a list
ls=list(i.values())
#declare an empty dictionary to catch the current iteration values
dict={}
#filling the current iteration dict values to the keys[]
for j in range(len(ls)):
dict[keys[j]]=ls[j]
#finally appending the current iteration temp dict to final ans list
ans.append(dict)
return ans