Python使用值更新嵌套列表和dict
我有以下模式,我想用对象值的base64更新值Python使用值更新嵌套列表和dict,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我有以下模式,我想用对象值的base64更新值contact和geo 例如: 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.o
contact
和geo
例如:
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"}]}]}]
虽然我希望动态地执行此操作,但此列表会变大,并且我希望能够更新这些字段。并使用替换的值重新生成相同的模式对象
代码:
更新:我想强调的是,我需要用替换的值重新构建相同的模式对象!而且,
数据
可以在列表中包含多个对象。我甚至都没有问为什么^^
我叫你的怪物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对所需零件进行编码的方法:
导入base64
#您需要将对象转换为字节
#在这里,我把它打印成一个字符串,并把它转换成字节,
#但还有其他选择,
#我真的不知道你为什么要这么做,事实就是这样
#很难说如何编码。
z=str(x[0]['details'][0]['location_details'][0]['contact'])。编码('utf-8')
#字节到base64编码
out=base64.b64编码(z).解码('utf-8')
打印(输出)
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()方法将字节作为参数,列表无法获取字节,因此我将列表
location\u detail.get(“geo”)
&location\u detail.get(“geo”)
转换为字符串。
要从字符串中获取字节,只需使用encode()方法
输出
_geo = b'W3snY2VudGVyJzogWyc1NS40J10sICdyb3RhdGUnOiAnNTAnLCAncGFyYWxsZWxzJzogJzYwMDAnfSwgeydjZW50ZXInOiBbJzU1LjQnXSwgJ3JvdGF0ZSc6ICc1MCcsICdwYXJhbGxlbHMnOiAnNjAwMCd9XQ=='
_contact = b'W3snY29udGFjdF9pbmZvcm1hdGlvbic6IFsnYUBnZXIub3JnJ10sICd0eXBlJzogJ2VtYWlsJywgJ3Bob25lJzogJzQ0NC00NC00NDQ0J30sIHsnY29udGFjdF9pbmZvcm1hdGlvbic6IFsnYkBnZXIub3JnJ10sICd0eXBlJzogJ2VtYWlsJywgJ3Bob25lJzogJzU1NS00NC00NDQ0J31d'
到目前为止,您的代码是什么?首先,谢谢您,其次,我认为您假设x只能有一个dict。但是x可以有多个dict x=[{},{}],然后只需使用
进行-循环,而不是[0]
,但如何用替换的值动态地重新构建相同的模式对象呢!另外,data
可以在列表中包含多个对象。对不起,“重建同一架构对象”是什么意思?就像我希望输出与以下内容完全相同一样[{“国家”:“德国”,“详细信息”:[{“国家”:“BR”,“位置\详细信息”:[{“zipcode”:“49875”,“联系人”:“W3TJB250YWN0x2LuzM9YBwF0Aw9UlthQgLCI5VCMDDLHR5CgU6ZW1HawWSCGHVBMU6NDQ0LTQ0LTQ0LTQ0NDR9LHTJB250YWN0x2LuzM9YBwF0Aw9UltiQgLCI5VCMDDLHR5CgU6ZW1HawWSCGHVBMU6NDR9NdQ0NdQ0LTQ0LTQ0NDR9LhTcxU9YBwWWWWWn0U2U2U0YWWWWWWWWWWnK2U2LgLdKm9YK9YWwWWWW0LbG9U5U5U5U5U5U5U5U5U5U5U5U5U5U5U5U5U5U5
不只是输出\u contact
和\u geo
这不是我期望的输出,我应该重建整个dict以及修改后的值。如何修改值并返回整个dict“数据“有修改吗?是什么阻止您将修改后的数据写入dict?毕竟它是一个可变的对象。。。否则您可能需要copy.deepcopy()
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'