我具有以下架构,我希望使用对象值的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.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
可以在列表中具有多个对象。
答案 0 :(得分:1)
我什至没有开始问为什么^^
我叫你的怪物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
答案 1 :(得分:0)
我不确定执行此操作的目的是什么,但是此代码将执行您想要的操作。
首先,您的代码中有一些错误,因此已在此处修复。
其次, b64encode()方法将字节作为参数,列表不会为您提供字节,因此我将列表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'