打印显示相同的字符串,但如果 == 失败

问题描述

我正在尝试比较 2 个字符串,但 == 运算符失败。如果您打印它们,它们似乎具有相同的值。即使类型是相同的:class str,print(repr()) 的输出是一样的,.strip() 也没有帮助,与 in 运算符的比较也失败。

字符串是“Neues Textdokument.txt - Edito”和编辑器的窗口名称

感谢@Random Davis 的建议,里面似乎是一个西里尔字母,看起来和拉丁字母一模一样。但是如果你检查字符串 a 和 b : 打印([ord(c) for c in a]) 打印([ord(c) for c in b]) 它以十进制显示字母的Unicode编号。它们分开在 e 和 M

解决方法

所以结果证明字符串只是看起来一样,但在实际数据中有一些意想不到的 unicode Cyrillic 字符,它们看起来与 ASCII 字符相同。因此,解决方案是运行以​​下代码将比较字符串与实际字符串进行比较:

from PyQt5.QtWidgets import QApplication,QMainWindow
import pyqtgraph as pg
import cv2


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        pg.setConfigOption('imageAxisOrder','row-major')
        pg.setConfigOption('leftButtonPan',False)  # if False,then dragging the left mouse button draws a rectangle

        self.grid = pg.GraphicsLayoutWidget()
        self.top_left = self.grid.addViewBox(row=1,col=1)
        self.top_left.setMouseEnabled(x=False,y=False)  # <--- Add this line

        image = cv2.imread('/path/to/your/image.jpg',cv2.IMREAD_UNCHANGED)
        self.image_item = pg.ImageItem()
        self.image_item.setImage(image)
        self.top_left.addItem(self.image_item)

        self.setCentralWidget(self.grid)


def main():
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()


if __name__ == '__main__':
    main()

这说明实际数据中存在西里尔字母“М”和“е”,导致字符串比较返回print([ord(c) for c in a]) print([ord(c) for c in b])