当.replace和.strip do n

2024-09-28 22:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python服务器,当我要求客户机的登录名检查它是否与数据库中的1有效时,出于某种原因,字符串的末尾总是有一个空格。我已经试过了:

newstring = oldstring.replace(' ', '')    #not working, white space is still there
newstring = oldstring.strip()  # also not working, whitespace is still there

#i also tried:
newstring = oldstring.replace('\n\t\r\s\f', '')    #not working, white space is still there

以下是来自服务器的代码片段:

client_loggedin = False
while client_loggedin == False:
    accountName = conn.recv(1024).decode("utf_8")
     print(accountName + " = " + str(len(accountName)))

      db = pymysql.connect("XXXXXXXXXXXXXsecretXXXXXXXXXX")
       cursor = db.cursor()
       cursor.execute("SELECT `XXXXXXXXXXXXX` FROM `XXXXXXXXXXXXX` WHERE `XXXXXXXXXXXXX`=%s", accountName)
       dataraw = cursor.fetchone()
       data = dataraw[0]
       db.close()
       if str(accountName) != str(data):

            print (str(data) + "+" + str(accountName))
            print("datalengte: " + str(len(data)) + " en " + "accountNamelengte: " + str(len(accountName)))
            print ("Deze gebruiker bestaat niet")
       elif accountName == data:
            reply = "Welcome " + str(accountName)
            conn.sendall(reply.encode("utf_8"))
            client_loggedin == True
            for x in clientList:
                if x != conn:
                    x.sendall((str(accountName) + " has joined the server").encode("utf_8"))

假设数据库中有一个帐户名oliver,客户机发送oliver。出于某种原因,我不知道字符串长度是7而不是6,这使得无法将值与数据库中的值进行比较


Tags: client数据库dataisnotcursorworkingthere