Skip to main content
 首页 » 编程设计

python之为什么这些字符串不相等 (Python)

2025年05月04日11lovecherry

def getValue(d, key): 
    for k, v in d.iteritems(): 
        print "{0} == {1}".format(k, key) 
        if k == key: 
            return v 
        elif isinstance(v, dict): 
            getValue(v, key) 
    logging.error("Cannot find key in dictionary") 
    return "" 
 
#d = getting the dictionary 
 
getValue(d, "error_frames") 

从我在函数中插入的打印语句,我清楚地看到“error_frames == error_frames”出现在控制台中,但是 if 语句没有被执行。为什么?该词典是通过使用模块 xmltodict 解析 xml 来构建的。

请您参考如下方法:

.format调用对象的__str__方法,不同对象的输出可以相同。

In [1]: a = 1 
 
In [2]: b = '1' 
 
In [3]: print '{0} == {1}'.format(a, b) 
1 == 1 
 
In [4]: a == b 
Out[4]: False