相同的字符串在 Python 中返回 FALSE 并带有 '==',为什么?

编程入门 行业动态 更新时间:2024-10-25 20:23:12
本文介绍了相同的字符串在 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"

推荐答案

无法重现您的问题.要调试它,打印或记录 data 和 action 的 repr():这可能会告诉你原因(可能是一些不可见的二进制字节已潜入 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 并带有 '==',为什么?

本文发布于:2023-11-06 09:54:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   Python   FALSE

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!