首页 > 解决方案 > Python update nested list and dicts with values

问题描述

I have the following schema I would like to update the values contact and geo with a base64 of the object values.

Example:

data = [{"country":"germany","details":[{"state":"BR","location_details":[{"zipcode":"49875","contact":[{"contact_information":["a@ger.org"],"type":"email","phone":"444-44-4444"},{"contact_information":["b@ger.org"],"type":"email","phone":"555-44-4444"}],"geo":[{"center":["55.4"],"rotate":"50","parallels":"6000"},{"center":["55.4"],"rotate":"50","parallels":"6000"}]}]}]}]

Expected output:

[{"country":"germany","details":[{"state":"BR","location_details":[{"zipcode":"49875","contact":"W3tjb250YWN0X2luZm9ybWF0aW9uOlthQGdlci5vcmddLHR5cGU6ZW1haWwscGhvbmU6NDQ0LTQ0LTQ0NDR9LHtjb250YWN0X2luZm9ybWF0aW9uOltiQGdlci5vcmddLHR5cGU6ZW1haWwscGhvbmU6NTU1LTQ0LTQ0NDR9XQo=","geo":"W3tjZW50ZXI6WzU1LjRdLHJvdGF0ZTo1MCxwYXJhbGxlbHM6NjAwMH0se2NlbnRlcjpbNTUuNF0scm90YXRlOjUwLHBhcmFsbGVsczo2MDAwfV0K"}]}]}]

though I want to do this dynamically, this list grows big and I want to be able to do update those fields. and rebuild the same schema object again with the substituted values.

Code:

import base64
for flat_data in data:
    for detail in data.get("details"):
        for location_detail in detail.get("location_details"):
            _contact = base64.b64encode(location_detail.get("paths"))
            _geo = base64.b64encode(location_detail.get("geo"))

Update: I want to emphasize that I need to rebuild the same schema object again with the substituted values! also, data can have multiple objects within the list.

标签: pythonpython-3.xdictionary

解决方案


I don't even start asking why^^ I call your monster x:

x = [{"country":"germany",
      "details":[{"state":"BR",
                  "location_details":[{"zipcode":"49875",
                                       "contact":[{"contact_information":["a@ger.org"],"type":"email","phone":"444-44-4444"},
                                                  {"contact_information":["b@ger.org"],"type":"email","phone":"555-44-4444"}],
                                       "geo":[{"center":["55.4"],"rotate":"50","parallels":"6000"},
                                              {"center":["55.4"],"rotate":"50","parallels":"6000"}]}]}]}]

This is a way to encode the part you want with base64:

import base64

# You need to convert your object to bytes
# Here, I printed it to a string and converted that to bytes,
# but there are other options,
# I can't really tell *why* you are doing it, so it is
# hard to say how to encode. 
z = str(x[0]['details'][0]['location_details'][0]['contact']).encode('utf-8')

# Bytes to base64 encoded
out = base64.b64encode(z).decode('utf-8') 

print(out)                        

json.dumps(...).encode('utf-8') might also be an alternative for encoding.

Edit

Now for recreating your object, you have two options. Either you don't need the original data or you do need to keep it. For the former, it would look like

import base64

for data in x:
   for detail in data['details']:
        for location_details in detail['location_details']:
            z = str(location_details['contact']).encode('utf-8')
            out = base64.b64encode(z).decode('utf-8') 
            location_details['contact'] = out

            z2 = str(location_details['geo']).encode('utf-8')
            out2 = base64.b64encode(z2).decode('utf-8') 
            location_details['geo'] = out2

print(x)                        

If you do need the original data, then you do first

import copy
x2 = copy.deepcopy(x)

and work the with x2


推荐阅读