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

时间:2015-04-23 18:09:03

标签: python arrays python-3.x

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)中的列表中显示每个在列表中使用“吸入器”的人,其年龄在另一列中,例如< / p>

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

1 个答案:

答案 0 :(得分:0)

遍历列表并显示(使用)该记录。无需使用变量 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