python 使用Unicode字符作为键将单个列表中的元素转换为键/值对

jaxagkaj  于 2022-12-17  发布在  Python
关注(0)|答案(1)|浏览(114)

我有一个列表(见下文),我希望将列表中包含Unicode字符(例如,'①'、'②'、'')的任何元素作为'category' JSON元素内的键/值对,并将列表中每个Unicode元素之间的以下元素作为'codes' JSON嵌套分组内的键/值对。
我有什么列表:

['①', 'Type of Care']
['SA', 'Substance use treatment']
['DT', 'Detoxification']
['HH', 'Transitional housing, halfway house, or sober home']
['SUMH', 'Treatment for co-occurring serious mental health illness/serious emotional disturbance and substance use disorders']
['②', 'Telemedicine']
['TELE', 'Telemedicine/telehealth']
['③', 'Service Settings (e.g., Outpatient, Residential, Inpatient, etc.)']
['HI', 'Hospital inpatient']
['OP', 'Outpatient']
['RES', 'Residential']
['HID', 'Hospital inpatient detoxification']
['HIT', 'Hospital inpatient treatment']
['OD', 'Outpatient detoxification']
['ODT', 'Outpatient day treatment or partial hospitalization']
['OIT', 'Intensive outpatient treatment']
['ORT', 'Regular outpatient treatment']
['RD', 'Residential detoxification']
['RL', 'Long-term residential']
['RS', 'Short-term residential']
['⑰', 'Assessment/Pre-treatment']
['CMHA', 'Comprehensive mental health assessment']
['CSAA', 'Comprehensive substance use assessment']
['ISC', 'Interim services for clients']
['OPC', 'Outreach to persons in the community']
['㉖', 'Facility Smoking Policy']
['SMON', 'Smoking not permitted']
['SMOP', 'Smoking permitted without restriction']
['SMPD', 'Smoking permitted in designated area']

我想创建的键/值对JSON:

{
    "codekey": [
        {
            "category": {
                "key": "①",
                "value": "Type of Care"
            },
            "codes": [
                {
                    "key": "SA",
                    "value": "Substance use treatment"
                },
                {
                    "key": "SA",
                    "value": "Substance use treatment"
                },
                {
                    "key": "DT",
                    "value": "Detoxification"
                },
                {
                    "key": "HH",
                    "value": "Transitional housing, halfway house, or sober home"
                },
                {
                    "key": "SUMH",
                    "value": "Treatment for co-occurring serious mental health illness/serious emotional disturbance and substance use disorders"
                }
            ]
        },
        {
            "category": {
                "key": "②",
                "value": "Telemedicine"
            },
            "codes": [
                {
                    "key": "TELE",
                    "value": "Telemedicine/telehealth"
                }
            ]
        },
        {
            "category": {
                "key": "③",
                "value": "Service Settings (e.g., Outpatient, Residential, Inpatient, etc.)"
            },
            "codes": [
                {
                    "key": "HI",
                    "value": "Hospital inpatient"
                },
                {
                    "key": "OP",
                    "value": "Outpatient"
                },
                {
                    "key": "RES",
                    "value": "Residential"
                },
                {
                    "key": "HID",
                    "value": "Hospital inpatient detoxification"
                },
                {
                    "key": "HIT",
                    "value": "Hospital inpatient treatment"
                },
                {
                    "key": "OD",
                    "value": "Outpatient detoxification"
                },
                {
                    "key": "ODT",
                    "value": "Outpatient day treatment or partial hospitalization"
                },
                {
                    "key": "OIT",
                    "value": "Intensive outpatient treatment"
                },
                {
                    "key": "ORT",
                    "value": "Regular outpatient treatment"
                },
                {
                    "key": "RD",
                    "value": "Residential detoxification"
                },
                {
                    "key": "RL",
                    "value": "Long-term residential"
                },
                {
                    "key": "RS",
                    "value": "Short-term residential"
                }
            ]
        },
        {
            "category": {
                "key": "⑰",
                "value": "Assessment/Pre-treatment"
            },
            "codes": [
                {
                    "key": "CMHA",
                    "value": "Comprehensive mental health assessment"
                },
                {
                    "key": "CSAA",
                    "value": "Comprehensive substance use assessment"
                },
                {
                    "key": "ISC",
                    "value": "Interim services for clients"
                },
                {
                    "key": "OPC",
                    "value": "Outreach to persons in the community"
                }
            ]
        },
        {
            "category": {
                "key": "㉖",
                "value": "Facility Smoking Policy"
            },
            "codes": [
                {
                    "key": "SMON",
                    "value": "Smoking not permitted"
                },
                {
                    "key": "SMOP",
                    "value": "Smoking permitted without restriction"
                },
                {
                    "key": "SMPD",
                    "value": "Smoking permitted in designated area"
                }
            ]
        }
    ]
}
deyfvvtc

deyfvvtc1#

希望下面的代码能有所帮助--这段代码迭代一个名为src_list的变量中的项,并使用字典创建一个JSON输出,如您所述。

import json

src_list = [['①', 'Type of Care'], ['SA', 'Substance use treatment'], ... ]
output_dicts = {"codekey": [] }
current_dict = None

for pair in src_list:
    # Is unicode character outside ASCII range? If so, it's defining a category
    if all(ord(c) > 128 for c in pair[0]):
        # If the current_dict is not None, we're onto a new category, so should add the last category to the output
        if (current_dict is not None):
            output_dicts["codekey"].append(current_dict)
        
        # Define new dict for this category
        current_dict = {
            "category": {
                "key": pair[0],
                "value": pair[1]
            },
            "codes": []
        }
    else:
        if (current_dict is not None):
            current_dict["codes"].append({
                "key": pair[0],
                "value": pair[1]
            })

output_dicts["codekey"].append(current_dict)
output_json = json.dumps(output_dicts, indent = 4, ensure_ascii=False)

print(output_json)

相关问题