I have a dataset called subscriptions which is a list of dictionaries:
subscriptions = [
#dict A
{"key1":"value",
..
"keyX":"value"},
#dict B
{"key1":"value",
..
"keyX":"value"},
]
For each of these dicts, I have a subset data "resources" which is also a list of dictionaries:
resources = [
#dict A
{"key1":"value",
..
"keyX":"value"},
#dict B
{"key1":"value",
..
"keyX":"value"},
]
For each item in subscriptions the dictionary keys may exist or not but the keys are known. Same goes for resources.
I am trying to append data from these two into a master_data list for the purpose of writing to a csv
for sub in subscriptions:
for res in resources:
master_data.append([sub.get('key')....res.get('key')])
with open("out.csv","w") as out_file:
writer = csv.writer(out_file, delimiter=",")
writer.writerows(master_data)
Is there a cleaner way to do this rather than getting each value because in total I have 44 items per row being appended to the master_data.
Edit:
As per request, keys information:
Subscription Keys:
dict_keys(['name', 'trial', 'status', 'startDate', 'serviceGate', 'lastBillDate', 'nextBillDate', 'autoRenewDate', 'serviceStatus', 'expirationDate', 'subscriptionId', 'autoRenewEnabled',
'billingTerms.autoRenewal.days', 'billingTerms.autoRenewal.type', 'billingTerms.billingModel', 'billingTerms.freezePrices', 'billingTerms.billingPeriod.unit', 'billingTerms.billingPeriod.duration', 'subscriptionPeriod.unit', 'subscriptionPeriod.duration', 'account.id'])
Resource Keys:
dict_keys(['resource.resourceId', 'resource.MPNumber', 'resource.internalResourceId', 'resource.status', 'resource.unitOfMeasureId', 'resource.measurable', 'resource.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'])
CodePudding user response:
Use a list comprehension for each dictionary.
master_data.append([sub.get(key) for key in subscription_keys] [res.get(key) for key in resource_keys])