繁体   English   中英

Python用值更新嵌套列表和字典

[英]Python update nested list and dicts with values

我有以下模式我想用对象值的 base64 更新值contactgeo

例子:

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"}]}]}]}]

预期输出:

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

尽管我想动态执行此操作,但此列表会变大,并且我希望能够更新这些字段。 并使用替换值再次重建相同的架构对象。

代码:

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"))

更新:我想强调的是,我需要使用替换值再次重建相同的架构对象! 此外, data在列表中可以有多个对象。

我什至不开始问为什么^^ 我称你的怪物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"}]}]}]}]

这是一种使用 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')也可能是编码的替代方法。

编辑

现在要重新创建您的对象,您有两个选择。 要么您不需要原始数据,要么确实需要保留它。 对于前者,它看起来像

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)                        

如果您确实需要原始数据,那么您首先需要

import copy
x2 = copy.deepcopy(x)

并与x2工作

我不确定这样做的目的是什么,但这段代码会做你想做的。

首先,您的代码中存在一些错误,因此已在此处修复。 其次, b64encode()方法将字节作为参数,list 不会为您获取字节,因此我将列表location_detail.get("geo") & location_detail.get("geo")为字符串。 要从字符串中获取字节,您只需使用encode()方法。

import base64

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"}]}]}]}]

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

输出

_geo = b'W3snY2VudGVyJzogWyc1NS40J10sICdyb3RhdGUnOiAnNTAnLCAncGFyYWxsZWxzJzogJzYwMDAnfSwgeydjZW50ZXInOiBbJzU1LjQnXSwgJ3JvdGF0ZSc6ICc1MCcsICdwYXJhbGxlbHMnOiAnNjAwMCd9XQ=='

_contact = b'W3snY29udGFjdF9pbmZvcm1hdGlvbic6IFsnYUBnZXIub3JnJ10sICd0eXBlJzogJ2VtYWlsJywgJ3Bob25lJzogJzQ0NC00NC00NDQ0J30sIHsnY29udGFjdF9pbmZvcm1hdGlvbic6IFsnYkBnZXIub3JnJ10sICd0eXBlJzogJ2VtYWlsJywgJ3Bob25lJzogJzU1NS00NC00NDQ0J31d'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM