相同的字符串使用 '==' 返回 FALSE在Python中,为什么?
数据字符串是通过套接字连接接收的。当收到第一个示例时,操作变量=“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法重现您的问题。要调试它,请打印或记录
data
和action
的repr()
:这可能会告诉您原因(可能是一些不可见的原因)二进制字节已潜入data
,具体取决于您获取它的方式[[您没有向我们展示]],因此潜入action
)。Can't reproduce your problem. To debug it, print or log the
repr()
ofdata
andaction
: this will likely show you the cause (probably some non-visible binary byte has snuck intodata
, based on how you obtained it [[which you don't show us]] and hence intoaction
).