Python Forum
I want to validate that there is not more than two blank spaces in a string of charac
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I want to validate that there is not more than two blank spaces in a string of charac
#1
I've already managed to remove the space at the beginning and end of the string with the function strip () but I do not know how to eliminate when in the text there are more than two spaces between words

for example:
a = '     this is the        test    '
a = a.split()
print a
'this is the        test'
Reply
#2
What kind of example is this?

Quote:Signature: str.split(self, /, sep=None, maxsplit=-1)
Docstring:
Return a list of the words in the string, using sep as the delimiter string.

sep
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
maxsplit
Maximum number of splits to do.
-1 (the default value) means no limit.
Type: method_descriptor

So You can't have string after using split() method. You get list.

>>> a = 'this is a test'
>>> a.split()
['this', 'is', 'a', 'test']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(Mar-29-2019, 02:19 PM)perfringo Wrote: What kind of example is this?

Quote:Signature: str.split(self, /, sep=None, maxsplit=-1)
Docstring:
Return a list of the words in the string, using sep as the delimiter string.

sep
The delimiter according which to split the string.
None (the default value) means split according to any whitespace,
and discard empty strings from the result.
maxsplit
Maximum number of splits to do.
-1 (the default value) means no limit.
Type: method_descriptor

So You can't have string after using split() method. You get list.

>>> a = 'this is a test'
>>> a.split()
['this', 'is', 'a', 'test']

sorry I was wrong to write it's a.sprit()
Reply
#4
Just spaces? What about newlines or tabs?

If you only care about spaces, a simple while loop would work, using str.replace repeatedly:
>>> a = '     this is the        test    '
>>> temp = None
>>> while a != temp or temp is None:
...   if temp is not None:
...     a = temp
...   temp = a.replace("  ", " ")
...
>>> a
' this is the test '
Or, if you want a "better" way, use a regex so it's just a single-pass through the string:
>>> import re
>>> a = '     this is the        test    '
>>> regex = re.compile(" {2,}")
>>> regex.sub(" ", a)
' this is the test '
Or, if you don't care about maintaining leading/trailing whitespace, you could continue to extend the str.split() you were using:
>>> a = '     this is the        test    '
>>> ' '.join(a.split())
'this is the test'
Reply
#5
You can use a counter. If you don't want multiple print statements per line of input. put the code in a function and return on the first greater-than-2-spaces found. Also you can use a.find(" ") <-- is actually 3 spaces, but that may not be specific enough.

str_ctr=0
a = '     this is the        test    ' 
for ltr in a:
    if ltr == " ":
        str_ctr += 1
        if str_ctr > 2:
            print("Too many spaces", a)
    else:
        str_ctr = 0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  validate large json file with millions of records in batches herobpv 3 1,275 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Create SQL connection function and validate mg24 1 947 Sep-30-2022, 07:45 PM
Last Post: deanhystad
Sad how to validate user input from database johnconar 3 1,917 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  Unable to Validate csv blanck data and write in csv prashant18 0 1,535 Jul-25-2020, 12:08 PM
Last Post: prashant18
  Validate JSON file BellaMac 12 5,377 Feb-27-2020, 03:17 PM
Last Post: snippsat
  CSV gives me blank row on PC, but not a Mac bazcurtis 2 2,798 Jan-06-2020, 08:40 AM
Last Post: buran
  building functions to validate strings , date etc metro17 2 2,370 Aug-08-2019, 12:42 PM
Last Post: ichabod801
  Python validate excel values data types Useruser00 0 4,840 Apr-08-2019, 01:29 PM
Last Post: Useruser00
  cross validate amilie1234 6 6,804 Feb-09-2017, 08:28 PM
Last Post: amilie1234
  Validate a List and print YES or NO Aoleone# 5 6,255 Nov-16-2016, 03:35 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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