【问题标题】:Trying to compare two strings. Fails because string is always prepended with u"foobar"试图比较两个字符串。失败,因为字符串总是以 u"foobar" 开头
【发布时间】:2018-05-01 11:07:32
【问题描述】:

我有两个列表

list1 = ["foo", "bar"]

list2 = ["foo", "bar"]

我有一个比较两个字符串的方法text_match

def text_match(self, expected, actual):
    if expected == actual:
        return True
    else:
    return False

我调用方法

self.text_match(list1[0], list2[0])

我错了。

当我从列表中打印两个字符串时

list1[0] >> foo

list2[1] >> u"foo"

为什么它们在打印时不一样? 我试过编码、解码、转换为字符串。 没有任何效果。

有什么想法吗?

【问题讨论】:

    标签: string python-2.7 list compare


    【解决方案1】:
    def text_match(self, expected, actual):
      if expected == actual:
          return True
      else:
          return False
    

    这个错误永远不会让脚本有机会运行

    return Flase 不能与 else 在同一个缩进中:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 2010-10-30
      • 2012-06-04
      • 2014-01-15
      相关资源
      最近更新 更多