【问题标题】:How to compare strings in python?如何比较python中的字符串?
【发布时间】:2016-12-23 23:15:03
【问题描述】:

我需要将一个字符串存储在一个元素数组中,当我存储和比较它们时它们是不一样的。我不确定我错过了什么。

test = np.zeros(1,dtype=[('data','a8')]) 

test['data']=str("right") 
print(test[0]['data'], test[0]['data'] == "right")  

我的输出是b'right' False。不过应该是True

【问题讨论】:

  • b'right' != 'right';您需要 .decode 使用适当编码的字节与字符串进行比较。例如,b'right'.decode('utf8') == 'right'

标签: python string compare string-comparison


【解决方案1】:

您正在比较字符串和字节(注意打印输出中的 b)。试试

print(test[0]['data'] == b"right")  

【讨论】:

  • 没问题。如果您满意,您应该接受答案。
猜你喜欢
  • 2022-09-30
  • 2019-01-17
  • 1970-01-01
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-19
  • 1970-01-01
相关资源
最近更新 更多