问题:
我有一个这种格式的txt文件:
Intestinal infectious diseases (001-003)
001 Cholera
002 Fever
003 Salmonella
Zoonotic bacterial diseases (020-022)
020 Plague
021 Tularemia
022 Anthrax
External Cause Status (E000)
E000 External cause status
Activity (E001-E002)
E001 Activities involving x and y
E002 Other activities
其中以 3-integer code/E+3-integer code/V+3-integer code 开头的每一行都是前面标题的值,它们是我字典的键。在我见过的其他问题中,可以使用列或冒号来解析每一行以生成键/值对,但是我的 txt 文件的格式不允许我这样做。
有没有办法将这样的txt文件制作成字典,其中键是组名,值是代码+疾病名称?
我还需要将代码和疾病名称解析到第二个字典中,所以我最终得到一个包含组名作为键的字典,值是第二个字典,代码作为键,疾病名称作为值。
脚本:
def process_file(filename):
myDict={}
f = open(filename, 'r')
for line in f:
if line[0] is not int:
if line.startswith("E"):
if line[1] is int:
line = dictionary1_values
else:
break
else:
line = dictionary1_key
myDict[dictionary1_key].append[line]
所需的输出格式是:
{"Intestinal infectious diseases (001-003)": {"001": "Cholera", "002": "Fever", "003": "Salmonella"}, "Zoonotic bacterial diseases (020-022)": {"020": "Plague", "021": "Tularemia", "022": "Anthrax"}, "External Cause Status (E000)": {"E000": "External cause status"}, "Activity (E001-E002)": {"E001": "Activities involving x and y", "E002": "Other activities"}}
慕桂英4014372
陪伴而非守候
守着一只汪
相关分类