简体   繁体   中英

Python - Check multiple white spaces in string

I am using this function to check if a string contains multiple white spaces:

def check_multiple_white_spaces(text):
    return "  " in text

and it is usually working fine, but not in this following code:

from bs4 import BeautifulSoup
from string import punctuation

text = "<p>Hello &nbsp; &nbsp; &nbsp;world!!</p>\r\n\r"

text = BeautifulSoup(text, 'html.parser').text
text = ''.join(ch for ch in text if ch not in set(punctuation))
text = text.lower().replace('\n', ' ').replace('\t', '').replace('\r', '')

print check_multiple_white_spaces(text)

The final value of text variable is hello world , but I don't know why the check_multiple_white_spaces function is returning False instead of True .

How can I fix this?

If you were to print the contents of text using repr() , you will see that it does not contain two consecutive spaces:

'hello \xa0 \xa0 \xa0world '

As a result, your function correctly returns False . This could be fixed by converting the non-break space into a space:

text = text.replace(u'\xa0', u' ')

First, your function check_multiple_white_spaces cannot really check if there is multiple white spaces as there could be three white spaces or more.

You should use re.search(r"\\s{2,}", text) .

Second, if you print text , you will find you need to unescape text.

See this answer.

How do I unescape HTML entities in a string in Python 3.1?

There is no consecutive space in text variable, that's why check_multiple_white_spaces function return False value.

>>> text
u'hello \xa0 \xa0 \xa0world '
>>> print text
hello      world 

\\xa0 is no-break space, non-breakable space (NBSP), hard space. Value os space is 32 and value of non-break space is 160

(u' ', 32)
(u'\xa0', 160)

The character \\xa0 is a NO-BREAK SPACE, and the closest ASCII equivalent would of course be a regular space.

Use unidecode module to convert all non-ASCII characters to their closest ASCII equivalent

Demo:

>>> import unidecode
>>> unidecode.unidecode(text)
'hello      world '
>>> "  " in unidecode.unidecode(text)
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM