-
-
Save elcolumbio/dfbbc26e2585425e2f4d47879bc9c584 to your computer and use it in GitHub Desktop.
# maybe its helpful or you see improvements | |
# i get lots of latin-1 encoded text fields, so i use this try block | |
def flatten_dict(d): | |
def items(): | |
for key, value in d.items(): | |
if isinstance(value, dict): | |
for subkey, subvalue in flatten_dict(value).items(): | |
try: | |
subvalue = subvalue.encode('latin-1').decode('utf-8') | |
except: | |
pass | |
yield key + "." + subkey, subvalue | |
elif isinstance(value, list): | |
for deep in value: | |
for xkey, xvalue in deep.items(): | |
if isinstance(xvalue, dict): | |
for xsubkey, xsubvalue in flatten_dict(xvalue).items(): | |
try: | |
xsubvalue = xsubvalue.encode('latin-1').decode('utf-8') | |
except: | |
pass | |
yield xkey + "." + xsubkey, xsubvalue | |
else: | |
try: | |
value.encode('latin-1').decode('utf-8') | |
except: | |
pass | |
yield key, value.encode('latin-1').decode('utf-8') | |
return dict(items()) | |
#i call it like that | |
for item in resultlist: | |
todflist.append(flatten_dict(item)) |
my yaml file looks like that (some reports i have to test):
refundeventlist :
toplevel_keys : [amazonorderid, marketplacename, posteddate, quantityshipped, sellerorderid, sellersku]
secondlevel_lists :
shipmentitemadjustmentlist:
itemchargeadjustmentlist : [chargetype, currencyamount, currencycode]
itemfeeadjustmentlist : [feetype, currencyamount, currencycode]
shipmenteventlist :
toplevel_keys : [amazonorderid, marketplacename, posteddate, quantityshipped, sellersku]
secondlevel_lists :
shipmentitemlist :
itemchargelist : [chargetype, currencyamount, currencycode]
itemfeelist : [feetype, currencyamount, currencycode]
chargebackeventlist :
toplevel_keys : [amazonorderid, marketplacename, posteddate, quantityshipped, sellersku]
secondlevel_lists :
shipmentitemadjustmentlist:
itemchargeadjustmentlist : [chargetype, currencyamount, currencycode]
itemfeeadjustmentlist : [feetype, currencyamount, currencycode]
servicefeeeventlist :
toplevel_keys : []
secondlevel_lists :
dummy:
feelist : [feetype, currencyamount, currencycode]
adjustmenteventlist :
toplevel_keys : [adjustmenttype, posteddate]
toplevel_lists :
adjustmentamount : [currencyamount, currencycode]
adjustmentitemlist : [quantity, sellersku]
productadspaymenteventlist :
toplevel_keys : [invoiceid, posteddate, transactiontype]
toplevel_lists :
taxvalue : [appendkey, currencycode, currencyamount]
transactionvalue : [appendkey, currencycode, currencyamount]
my new version is more explicit. It looks horrible:
`class ApiResponseParser():
# we parse the response and map keys to our mapping yaml file
# the flow is we fill a single dictionary through kombination 1-3
def init(self):
self.path_to_response = 'masterK.yaml'
self.path_to_mappings = '/home/flo/miniconda3/lib/python3.6/site-packages/mws/wrapper/finance_mapping.yaml'
self.response = dict()
self.mappings = dict()
self.multirow_resultlist = []
self.resultlist = []
self.main()