相同的字符串在 Python 中返回 FALSE 并带有 '==',为什么? [英] Identical string return FALSE with '==' in Python, why?

查看:34
本文介绍了相同的字符串在 Python 中返回 FALSE 并带有 '==',为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过套接字连接接收数据字符串.当收到第一个动作变量将 = 'IDENTIFY' 的示例时,它起作用了.但是当收到第二个例子时,其中的 action variable will = 'MSG' 它不会比较.

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.

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

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 = '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"

推荐答案

无法重现您的问题.要调试它,打印或记录 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).

这篇关于相同的字符串在 Python 中返回 FALSE 并带有 '==',为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆