0

我有一本字典,它是 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"])
[]
4

1 回答 1

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
于 2020-05-26T18:45:09.770 回答