[英]Python - Check multiple white spaces in string
我正在使用此函數來檢查字符串是否包含多個空格:
def check_multiple_white_spaces(text):
return " " in text
並且通常可以正常工作,但是不能在以下代碼中進行操作:
from bs4 import BeautifulSoup
from string import punctuation
text = "<p>Hello 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)
text
變量的最終值是hello world
,但是我不知道為什么check_multiple_white_spaces
函數返回False
而不是True
。
我怎樣才能解決這個問題?
如果要使用repr()
打印text
內容,您將看到它不包含兩個連續的空格:
'hello \xa0 \xa0 \xa0world '
結果,您的函數正確返回False
。 這可以通過將不間斷空格轉換為空格來解決:
text = text.replace(u'\xa0', u' ')
首先,您的函數check_multiple_white_spaces
不能真正檢查是否存在多個空格,因為可能存在三個或更多個空格。
您應該使用re.search(r"\\s{2,}", text)
。
其次,如果您打印text
,您將發現需要取消轉義文本。
看到這個答案。
text
變量中沒有連續的空格,這就是為什么check_multiple_white_spaces
函數返回False
的原因。
>>> text
u'hello \xa0 \xa0 \xa0world '
>>> print text
hello world
\\xa0
是不間斷空間,不間斷空間(NBSP),硬空間。 os空間的值為32,非中斷空間的值為160
(u' ', 32)
(u'\xa0', 160)
字符\\ xa0是一個NO-BREAK空格,最接近的ASCII等效詞當然是常規空格。
使用unidecode module
將所有非ASCII字符轉換為與其最接近的ASCII等效字符
演示:
>>> import unidecode
>>> unidecode.unidecode(text)
'hello world '
>>> " " in unidecode.unidecode(text)
True
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.