为什么以下字符串比较不起作用?
我有以下代码(我修改了它以使其更简单)。 我从数据库中检索出一个玩家并将其添加到玩家列表中。 然后我循环遍历玩家列表并尝试找到它,即使字符串相同,比较也返回 false..
def findPlayer2(self, protocol, playerId):
cur = self.conn.cursor()
cur.execute("SELECT playerId, playerName, rating FROM players WHERE playerId LIKE (%s)", [playerId])
nbResult = cur.rowcount
result = cur.fetchone()
cur.close()
if nbResult > 0:
player = Player(protocol, str(result[0]), str(result[1]), result[2])
self.players.append(player)
for player in self.players:
id1 = str(player.playerId)
id2 = str(playerId)
print type(id1)
print type(id2)
print "Comparing '%s' with '%s'" % (id1, id2)
# HERE IS THE COMPARISON
if id1 == id2:
print "Equal! Player found!"
return player
else:
print "Not equal :("
给出以下结果:
<type 'str'>
<type 'str'>
Comparing '11111111' with '11111111'
Not equal :(
%r
代替'%s'
。可能存在一些不可打印的字符或相似字符。 - user395760