我有一個叫做subscriptions的資料集,它是一個字典串列:
subscriptions = [
#dict A
{"key1"/span>:"value"/span>,
..
"keyX":"value"}。
#dict B
{"key1":"value"。
..
"keyX":"value"}。
]
對于每一個字典,我都有一個子集資料 "資源",它也是一個字典的串列:
resources = [
#dict A
{"key1"/span>:"value"/span>,
..
"keyX":"value"}。
#dict B
{"key1":"value"。
..
"keyX":"value"}。
]
對于訂閱中的每個專案,字典中的鍵可能存在,也可能不存在,但鍵是已知的。 資源也是如此。
我試圖將這兩個專案的資料追加到一個master_data串列中,以便寫入csv
。for sub in subscriptions。
for res in resources:
master_data.append([sub.get('key')....res.get('key')]
with open("out.csv"/span>,"w"/span>) as out_file:
writer = csv.writer(out_file, delimiter=", ")
writer.writerows(master_data)
是否有一個更干凈的方法來做,而不是獲取每個值,因為我總共有44個專案每行被附加到master_data上。
編輯:
按照要求,鍵資訊:
Subscription Keys:
dict_keys(['name', 'trial', 'status', 'startDate'/span>, 'serviceGate'/span>, 'lastBillDate'/span>, 'NextBillDate', 'autoRenewDate', 'serviceStatus', 'expirationDate', ' subscriptionId', 'autoRenewEnabled',
'billingTerms.autoRenewal.day', 'billingTerms.autoRenewal.type', 'billingTerms. billingModel', 'billingTerms.freezePrices', 'billingTerms. billingPeriod.unit', 'billingTerms.billingPeriod.duration', 'subscriptionPeriod.unit', 'subscriptionPeriod.duration', 'account.id'] )
資源鍵。
dict_keys(['resource.resourceId', 'resource.MPNumber', 'resource.internationalResourceId', 'resource. 狀態', '資源.unitOfMeasureId', '資源.可測量', '資源.showInCustomerPanel',
'resource.allowToModifyInTrial', 'resource.included', 'resource.additional', 'resource. ordered', 'resource.min', 'resource.max', 'resource.vendorSubscriptionId', 'resource. usage', 'resource.consumptionType', 'resource.name.en_US', 'resource.name.fr_FR', 'resource. name.nl_NL', ' resource.unitOfMeasure.en_US', ' resource.unitOfMeasure.fr_FR', ' resource.unitOfMeasure.nl_NL'] )
uj5u.com熱心網友回復:
為每個字典使用一個串列理解。
master_data.append([sub.get(key) for key in subscription_keys] [res.get(key) for key in resources_keys])
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/328609.html
標籤:
下一篇:如何生成隨機概率分布julia
