如何从文本文件的列表中查找内容

2024-03-29 12:13:18 发布

您现在位置:Python中文网/ 问答频道 /正文

with open ("patientlist.txt","r") as file:
    patientlist = [line.split(",")for line in file]
for patient in patientlist:
    patient[-1] = patient[-1].replace('\n',"")

def medicine():
    health=input("choose the medicine you want to display clients for")
    vMember=False
    while vMember==False:
        for m in patientlist:
            if m[0] == health:
                vMember=True

从这里,我可以添加什么,以便从文本文件(patientlist)的列表中,您可以在列表中显示每个使用“吸入器”的人,并在另一列中显示他们的年龄,例如

paul,50,antidepressants
liz,24,inhaler
jack,30,epipen

Tags: intxtfalse列表foraswithline
1条回答
网友
1楼 · 发布于 2024-03-29 12:13:18

遍历列表并显示(使用)该记录。无需使用变量vMember。以您的方式使用vMember将在患者首次使用“吸入器”时停止迭代。你知道吗

def medicine():
    health=input("choose the medicine you want to display clients for")
    for m in patientlist:
        if m[2] == health:    # Note, change of m[0](name) to m[2](health)
            print m

相关问题 更多 >