python说苹果不等于苹果

时间:2018-11-25 19:25:22

标签: python django python-3.x

在我的django代码中我遇到了一个奇怪的情况,python拒绝接受两个字符串相等:

def depticon(depta):
    print(f'depta is {depta}')
    print(f'Testing |{depta}| against |Ear, Nose, Throat|')
    if depta=="Ear, Nose, Throat":        
        icon = "ear.png"
        print("Matched ENT")
    else:
        print("No match")
        icon = "health-sign.png"
    print(f'Icon is {icon}')
    return icon

这就是它的称呼方式。在这里,诊所是一个查询集:

specialties = []
specialtytext = []
specialtyicon = []
for clinic in clinics:
    if clinic.doctorid.dept not in specialties:
        dept = clinic.doctorid.department
        specialties.append(dept)
        specialtytext.append(depttext(dept))
        specialtyicon.append(depticon(dept))

查看输出:

depta is Ear, Nose, Throat
Testing |Ear, Nose, Throat| against |Ear, Nose, Throat|
No match
Icon is health-sign.png

我将|字符放在字符串周围,以检查字符串中是否有任何特殊字符。

1 个答案:

答案 0 :(得分:4)

我怀疑depta参数的类型不是str,这就是为什么它与str值“ Ear,Nose,Throat”不匹配的原因。在进一步使用之前,请尝试将department的值传递给str()

有趣的事实:print()函数自动使用str()强制转换参数。