问题描述
我正在处理一个项目,我需要将数据库中的一组数据行转换为 list of OrderedDict
以用于其他目的,并使用此 list of OrderedDict
转换为 nested JSON
格式python
。我开始学习python。我能够将查询响应从 list of lists
数据库转换为 list of OrderedDict
。
我有 list of OrderedDict
如下:
{
'OUTBOUND': [
OrderedDict([('Leg',1),('SessionID','W12231fwfegwcaa2'),('FeeCode','ATO'),('SeatGroup','2'),('Currency','MXN'),('Modality','VB'),('BookingClass','A'),('Price',145.0),('Num_pax',('Channel','Web')]),OrderedDict([('Leg','4'),111.0),'BDM'),'null'),2),'1'),'U'),180.0),'Web'))]),97.0),'Web')])
]
}
我需要如下嵌套格式:
{
"OUTBOUND": [
{
"Leg": 1,"SessionID": "W12231fwfegwcaa2","Modality": "VB","BookingClass": "A","FeeCodes":[
{
"FeeCode": "ATO","Prices":
[
{
"SeatGroup": "2","Price": 145.0,"Currency": "MXN"
},{
"SeatGroup": "4","Price": 111.0,"Currency": "MXN"
}
]
},{
"FeeCode": "VBABDM","Prices":
[
{
"SeatGroup": "null","Currency": "MXN"
}
]
}
],"Num_pax": 1,"Channel": "Web"
},{
"Leg": 2,"BookingClass": "U","Prices":
[
{
"SeatGroup": "1","Price": 180.0,"price": 97.0,"Channel": "Web"
}
]
}
如果我没记错的话,我需要按Leg
、SessionID
、Modality
、BookingClass
、Numpax
和Channel
分组并将 FeeCode
、SeatGroup
、Price
和 Currency
分组为上述嵌套格式,但无法继续了解如何循环和分组嵌套。
如果我能得到一些帮助就太好了。谢谢
解决方法
我能够编写一个 python 代码来获取我需要的格式,使用简单的循环在输出中进行一些更改,例如将字段 SessionID、Num_Pax 和 Channel 取出,然后生成 OUTBOUND 字段和其中的字段。
我使用了一个列表列表作为输入,而不是 OrderedDict,我将其转换为 Pandas DataFrame 并使用 DataFrame 来获取嵌套格式。
以下是我使用的代码:
outbound_df = pd.DataFrame(response_outbound,columns=All_columns)
Common_columns = ['Leg','Modality','BookingClass']
### Taking SessionID,AirlineCode,Num_Pax and Channel outside OUTBOUND part as they are common for all the leg level data
response_data['SessionID'] = outbound_df['SessionID'].unique()[0]
response_data['Num_Pax'] = int(outbound_df['Num_Pax'].unique()[0])
response_data['Channel'] = outbound_df['Channel'].unique()[0]
temp_data = []
Legs = outbound_df['Leg'].unique()
for i in Legs:
subdata = outbound_df[outbound_df['Leg']==i]
### Initializing leg_data dict
leg_data = collections.OrderedDict()
### Populating common fields of the leg (Leg,Modality,BookingClass)
for j in Common_columns:
if(j=='Leg'):
leg_data[j] = int(subdata[j].unique()[0])
else:
leg_data[j] = subdata[j].unique()[0]
leg_data['FeeCodes'] = []
FeeCodes = subdata['FeeCode'].unique()
for fc in FeeCodes:
subdata_fees = subdata[subdata['FeeCode']==fc]
Prices = {'FeeCode':fc,"Prices":[]}
for _,rows in subdata_fees.iterrows():
data = {}
data['SeatGroup'] = rows['SeatGroup']
data['Price'] = float(rows['Price'])
data['Currency'] = rows['Currency']
Prices["Prices"].append(data)
leg_data["FeeCodes"].append(Prices)
temp_data.append(leg_data)
response_data["OUTBOUND"] = temp_data
我可以在 json.dumps
上执行 response_data
以获得 json 格式,该格式将发送到下一步。
以下是我得到的输出格式:
{
"SessionID":"W12231fwfegwcaa2","Num_Pax":1,"Channel":"Web","OUTBOUND":[
{
"Leg":1,"Modality":"VB","BookingClass":"A","FeeCodes":[
{
"FeeCode":"ATO","Prices":[
{
"SeatGroup":"2","Price":145.0,"Currency":"MXN"
},{
"SeatGroup":"4","Price":111.0,"Currency":"MXN"
}
]
},{
"FeeCode":"VBABDM","Prices":[
{
"SeatGroup":"null","Currency":"MXN"
}
]
}
]
},{
"Leg":2,"BookingClass":"U","Prices":[
{
"SeatGroup":"1","Price":180.0,"price":97.0,"Currency":"MXN"
}
]
}
]
}
]
}
如果我们可以在冗长的迭代或任何其他更改方面缩短代码,请告诉我。谢谢。 PS:抱歉我的编辑错误
,假设您将字典存储到某个变量 foo
,您可以:
import json
json.dumps(foo)
请注意,您在第 4 个元素 OUTBOUND
列表中添加了额外的括号