我有一个字典,它是一个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"])
[]
我找到了答案。似乎multiprocessing.managers.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