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

标签 python

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

关于python - 为什么这些字符串不相等? (Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10800942/

相关文章:

python - 'python setup.py install' 和 'pip install' 之间的区别

python pandas 标志如果列中的每个值有多个唯一行

python - 如何以正确的方式在 Python 中连接到 MySQL 数据库?

python - 我如何展平字典的深度仅超过 3 个级别

python - 在 gnuplot 中绘制 pm3d map 的 python 类比是什么?

python - 属性错误 : 'Ui_MainWindow' object has no attribute 'setCentralWidget'

c++ - C++ 和 Python 之间的简单 IPC(跨平台)

Python lxml 防止小于 < 或大于 > 被转换为 &lt 和 &gt

python - 使用 itertools.product 在范围内重复

python - 如何对列表中的两个最大元素求和?