Python Forum
How to remove whitespace from a string when .replace and .strip do not work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to remove whitespace from a string when .replace and .strip do not work
#1
I have a python server and when i ask the client the login name to check it is valid with the 1 in the database, for some reason there is always a whitespace at the end of the string. I already tried this:
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
Here is a code snippet from the server:
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"))
Let's say there is an account name oliver in the data base and the client sends oliver. For some some reason i don't know is the string length 7 and not 6 and this makes comparing the value with the value from the database imposseble
Reply
#2
try this
Remove ALL spaces in a string, even between words:

import re
sentence = re.sub(r"\s+", "", sentence, flags=re.UNICODE)
Remove spaces in the BEGINNING of a string:

import re
sentence = re.sub(r"^\s+", "", sentence, flags=re.UNICODE)
Remove spaces in the END of a string:

import re
sentence = re.sub(r"\s+$", "", sentence, flags=re.UNICODE)
Remove spaces both in the BEGINNING and in the END of a string:

import re
sentence = re.sub("^\s+|\s+$", "", sentence, flags=re.UNICODE)
Remove ONLY DUPLICATE spaces:

import re
sentence = " ".join(re.split("\s+", sentence, flags=re.UNICODE))
Reply
#3
I tried and none of that is working. The string keeps having that additional space at the end.
I tried many many different things. None of them works
Reply
#4
What does print(repr(accountName)) return? Since you are using utf-8, I expect there's a unicode character in there that strip doesn't recognize as white space.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
it returned this in the console:
'oliver\x00'
Reply
#6
Looks like a null unicode character. Try accountName = accountName.replace('\x00', '').
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#7
Jep i already tried:
accountName = accountName.replace('\x00','')
Thank you very much
Reply
#8
Hm, this is strange. Usually a null byte is not printed as a whitespace.
It's used often as delimiter with the find command piped to xargs.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  remove gilberishs from a "string" kucingkembar 2 202 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
  Need to replace a string with a file (HTML file) tester_V 1 698 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replace string in a nested Dictianory. SpongeB0B 2 1,147 Mar-24-2023, 05:09 PM
Last Post: SpongeB0B
  Replace with upper(string) WJSwan 7 1,545 Feb-10-2023, 10:28 AM
Last Post: WJSwan
  extract only text strip byte array Pir8Radio 7 2,786 Nov-29-2022, 10:24 PM
Last Post: Pir8Radio
Smile please help me remove error for string.strip() jamie_01 3 1,145 Oct-14-2022, 07:48 AM
Last Post: Pedroski55
  Remove a space between a string and variable in print sie 5 1,705 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  Find and Replace numbers in String giddyhead 2 1,196 Jul-17-2022, 06:22 PM
Last Post: giddyhead
  How do I remove spurious "." from a string? Zuhan 7 1,961 Apr-12-2022, 02:06 PM
Last Post: Pedroski55
  Can't strip extra characters from Data Canflyguy 7 1,813 Jan-10-2022, 02:16 PM
Last Post: Canflyguy

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020