我并不擅长将列表的某些部分转换为嵌套的Json,希望能得到一些指导。我有一个包含如下数据的列表:
['<h5> 1|',
'<h6>Type of Care|',
'<h6>SA|Substance use treatment|',
'<h6>DT|Detoxification |',
'<h6>HH|Transitional housing, halfway house, or sober home|',
'<h6>SUMH|Treatment for co-occurring serious mental health | illness/serious emotional disturbance and substance | use disorders|',
'',
'<h5> 2|',
'<h6>Telemedicine|',
'<h6>TELE|TelemedicineTelemedicine/telehealth|',
'']
我想首先删除列表中没有内容的所有记录,然后我想将包含““这样的标记的记录转换为键,并将包含““的记录分组为如下json输出所示的值:
"codekey": [
{
"category": [
{
"key": 1,
"value": "Type of Care"
}
],
"codes": [
{
"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": 2,
"value": "Telemedicine"
}
],
"codes": [
{
"key": "TELE",
"value": "TelemedicineTelemedicine/telehealth"
}
]
}
]
我想我需要执行一个循环,但我在如何创建“键/值”关系上遇到了麻烦。我想我还需要使用正则表达式,但我不是Python中最好的,无法从概念上将数据转换为所需的输出。有什么关于培训的建议可以让我做这件事吗?或者有什么关于如何开始的初步建议吗?谢谢!
1条答案
按热度按时间ogq8wdun1#
考虑到您的格式保持不变。这里有一个灵活的解决方案,可配置:
提供的示例输入的输出: