添加将字典附加到另一个 multiprocessing.managers.DictProxy 中的列表
Posted
技术标签:
【中文标题】添加将字典附加到另一个 multiprocessing.managers.DictProxy 中的列表【英文标题】:Adding appending a dictionary to a list inside another multiprocessing.managers.DictProxy 【发布时间】:2020-09-13 14:37:10 【问题描述】:我有一个字典,它是 multiprocessing.managers.DictProxy 。字典看起来像:
manager = multiprocessing.Manager()
data_set = manager.dict()
def write_company(company):
try:
entity = company["registration"]["name"]
except Exception:
entity = ""
pass
try:
registration_number = company["registration"]["registration_number"]
except Exception:
registration_number = ""
pass
try:
fei_number = company["registration"]["fei_number"]
except Exception:
fei_number = ""
pass
try:
operator_number = company["registration"]["owner_operator"]["owner_operator_number"]
except Exception:
operator_number = ""
pass
try:
iso_country_code = company["registration"]["iso_country_code"]
country_name = pycountry.countries.get(alpha_2=iso_country_code).name #Retrieves country name
except Exception:
country_name = ""
pass
try:
city = company["registration"]["city"]
except Exception:
city = ""
pass
try:
zipcode = company["registration"]["postal_code"]
except Exception:
zipcode = ""
pass
try:
address = company["registration"]["address_line_1"]
except Exception:
address = ""
pass
try:
address2 = company["registration"]["address_line_2"]
except Exception:
address2 = ""
pass
try:
state_code = company["registration"]["state_code"]
except Exception:
state_code = ""
pass
try:
reg_expiry_date_year = company["registration"]["reg_expiry_date_year"]
reg_expiry_date_year = reg_expiry_date_year + "/12/31"
except Exception:
reg_expiry_date_year = ""
pass
try:
establishment_type = company["establishment_type"][0]
except Exception:
establishment_type = ""
pass
data_set[(registration_number, entity, operator_number)] =
"entity": entity,
"registration_name": registration_number,
"operator_number": operator_number,
"fei_number": fei_number,
"country_name": country_name,
"city": city,
"zipcode": zipcode,
"address": address,
"address2": address2,
"state": state_code,
"reg_expiry_date_year": reg_expiry_date_year,
"fda_product": list()
在另一个函数中,我为fda_product
列表收集产品,然后将其附加到列表中。
def write_products(key, products):
for product in products:
#Establishment Type
establishment_type = ""
try:
length = len(product["establishment_type"])
for i in range(length):
establishment_type += product["establishment_type"][i] + ";"
except Exception:
pass
product_name = ""
try:
len_proprietary = len(product["proprietary_name"])
for i in range(len_proprietary):
product_name += product["proprietary_name"][i] + ";"
except Exception:
pass
try:
pma_number = product["pma_number"]
except Exception:
pma_number = ""
pass
try:
k_number = product["k_number"]
except Exception:
k_number = ""
pass
try:
product_code = [item['product_code'] for item in product["products"]][0]
except Exception:
product_code = ""
pass
try:
created_date = [item['created_date'] for item in product["products"]][0]
except Exception:
created_date = ""
pass
try:
device_name = [item["openfda"]['device_name'] for item in product["products"]][0]
except Exception:
device_name = ""
pass
try:
exempt = [item["exempt"] for item in product["products"]][0]
except Exception:
exempt = ""
pass
product_data =
"product_name": product_name,
"product_code": product_code,
"k_number": k_number,
"pma_number": pma_number,
"exempt": exempt,
"device_name": device_name,
"created_date": created_date,
"establishment_type": establishments
data_set[key]["fda_product"].append(product_data)
我已经测试确保产品数据里面有数据并且密钥是正确的。当我打印列表时,列表不会附加到列表中,结果是一个空列表。谢谢你的帮助
print(product_data)
'k_number': u'', 'created_date': u'1991-07-16', 'exempt': u'', 'pma_number': u'', 'establishment_type': u'Develop Specifications But Do Not Manufacture At This Facility;', 'product_code': u'EIY', 'product_name': u'PREMIER PLASTIC FILLIN;', 'device_name': u'Instrument, Filling, Plastic, Dental'
print(data_set[key]["fda_product"])
[]
【问题讨论】:
我认为我们无法找出您提供给我们的内容的问题。你在哪里检查数据集的内容? data_set 还会发生什么?您是否尝试使用调试器?只要您遇到同样的问题,请复制您的代码并减少它。然后在这里发布。 @ominug 感谢您指出这一点。我已经更新以详细说明 【参考方案1】:我找到了答案。好像 multiprocessing.managers.DictProxy 不能直接更新。要完成这项任务,您必须获得字典的本地副本。更新本地副本并将其添加回 DictProxy。这就是 write_product() 函数的代码样子
def write_products(key, products):
fda_product = []
for product in products:
...
...
...
product_data =
"product_name": product_name,
"product_code": product_code,
"k_number": k_number,
"pma_number": pma_number,
"exempt": exempt,
"device_name": device_name,
"created_date": created_date,
"establishment_type": establishment_type
fda_product.append(product_data)
local_dict = data_set[key]
local_dict["fda_product"] = fda_product
data_set[key] = local_dict
【讨论】:
以上是关于添加将字典附加到另一个 multiprocessing.managers.DictProxy 中的列表的主要内容,如果未能解决你的问题,请参考以下文章