亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

python字符串比較(==)不起作用

python字符串比較(==)不起作用

森林海 2021-03-23 17:13:39
為什么以下字符串比較不起作用?我有以下代碼(我對其進行了修改以使其更簡單)。我從數據庫中檢索一個播放器,并將其添加到播放器列表中。然后,我在播放器列表上循環并嘗試找到它,即使字符串相同,比較也返回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 :(
查看完整描述

3 回答

?
明月笑刀無情

TA貢獻1828條經驗 獲得超4個贊

您似乎有一個字符串處理錯誤。 PlayerId似乎是一個存儲在Unicode字符串中的C字符串。


背景:C使用空字節(\x00)標記字符串的結尾。由于此nullbyte位于您的字符串中,因此最終以該對象的字符串表示形式結束。


您可以在這里看看,以供參考。但是如果沒有更多代碼,我不確定原因/解決方法。


你試過了type(playerId)嗎?


編輯:我不知道您正在使用什么python實現,對于cpython看看這里


不幸的是,我并不是很確定c和python的接口,但是您可以嘗試使用PyString_FromStringc端將其轉換為python字符串,或者使用一些手工函數(例如,在第一個未轉義的0處使用regex進行拆分)。


此探視器中列出了一些接口庫


查看完整回答
反對 回復 2021-03-30
  • 3 回答
  • 0 關注
  • 366 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號