Bottom Page

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
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]
                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)
                    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
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))
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
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 -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

it returned this in the console:
Looks like a null unicode character. Try accountName = accountName.replace('\x00', '').
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Jep i already tried:
accountName = accountName.replace('\x00','')
Thank you very much
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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Highlight and remove specific string of text itsalmade 5 260 Dec-11-2019, 11:58 PM
Last Post: micseydel
  Cannot Remove the Double Quotes on a Certain Word (String) Python BeautifulSoup soothsayerpg 5 520 Oct-27-2019, 09:53 AM
Last Post: newbieAuggie2019
  Replace string in many files in a folder metro17 8 449 Oct-16-2019, 06:46 PM
Last Post: ndc85430
  with input remove a string from the list konsular 3 235 Oct-12-2019, 09:25 AM
Last Post: konsular
  split strip issues 'NonType' casacafe 8 284 Oct-08-2019, 06:29 PM
Last Post: ichabod801
  open, read and replace a string in a file Reims 0 236 Oct-02-2019, 01:30 PM
Last Post: Reims
  Read each line, replace string and save into a new file igormonteiro 2 415 Sep-15-2019, 01:24 PM
Last Post: buran
  removing spaces/tabs after used .strip() zarize 0 163 Sep-11-2019, 12:46 PM
Last Post: zarize
  strip off just newlines Skaperen 11 663 Jun-19-2019, 06:28 PM
Last Post: Skaperen
  replace white space with a string, is this pythonic? Skaperen 1 332 Jun-18-2019, 11:36 PM
Last Post: metulburr

Forum Jump:

Users browsing this thread: 1 Guest(s)