相同的字符串使用 '==' 返回 FALSE在Python中,为什么?

发布于 2024-08-05 22:32:58 字数 418 浏览 2 评论 0原文

数据字符串是通过套接字连接接收的。当收到第一个示例时,操作变量=“IDENTIFY”,它起作用了。但是,当收到第二个示例时,其中操作变量=“MSG”,它不会进行比较。

最奇怪的是,当我使用 Telnet 而不是我的套接字客户端时,两者都被成功比较。但字符串是相同的...是否有可能字符串的编码方式不同?我怎么知道?

数据示例:

data = 'IDENTIFY 54143'
or
data = 'MSG allo'

action = data.partition(' ')[0]
if action == "MSG":
    self.sendMessage(data)
elif action == "IDENTIFY":  
    self.sendIdentify(data)
else:
    print "false"

The data string is receive through a socket connexion. When receiving the first example where action variable would = 'IDENTIFY', it works. But when receiving the second example where action variable would = 'MSG' it does not compare.

And the most bizarre thing, when I use Telnet instead of my socket client both are being compare successfully. But the string are the same... Is there a possibility that the string are not encode in the same way? How can I know?

data example:

data = 'IDENTIFY 54143'
or
data = 'MSG allo'

action = data.partition(' ')[0]
if action == "MSG":
    self.sendMessage(data)
elif action == "IDENTIFY":  
    self.sendIdentify(data)
else:
    print "false"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-08-12 22:32:58

无法重现您的问题。要调试它,请打印或记录 dataactionrepr():这可能会告诉您原因(可能是一些不可见的原因)二进制字节已潜入data,具体取决于您获取它的方式[[您没有向我们展示]],因此潜入action)。

Can't reproduce your problem. To debug it, print or log the repr() of data and action: this will likely show you the cause (probably some non-visible binary byte has snuck into data, based on how you obtained it [[which you don't show us]] and hence into action).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文