在 Python 中從字串中刪除 xa0 的方法
Najwa Riyaz
2023年1月30日
Python
Python String
-
使用 Unicodedata 的
Normalize()函式從 Python 中的字串中刪除\xa0 -
使用字串的
replace()函式從 Python 中的字串中刪除\xa0 -
使用
BeautifulSoup庫的get_text()函式將strip設為True從 Python 中的字串中刪除\xa0
本文介紹了在 Python 中從字串中刪除 \xa0 的不同方法。
\xa0 Unicode 代表程式中的硬空間或不間斷空間。它表示為 在 HTML 中。
可以幫助從字串中刪除 \xa0 的 Python 函式如下。
unicodedata的normalize()函式- 字串的
replace()函式 BeautifulSoup庫的get_text()函式將strip’ 設為True。
使用 Unicodedata 的 Normalize() 函式從 Python 中的字串中刪除 \xa0
你可以使用 unicodedata 標準庫的 unicodedata normalize() 函式從字串中刪除 \xa0。
normalize() 函式使用如下。
unicodedata.normalize("NFKD", string_to_normalize)
這裡,NFKD 表示 normal form KD。它將所有相容字元替換為其等效字元。
下面的示例程式說明了這一點。
import unicodedata
str_hard_space = "17\xa0kg on 23rd\xa0June 2021"
print(str_hard_space)
xa = u"\xa0"
if xa in str_hard_space:
print("xa0 is Found!")
else:
print("xa0 is not Found!")
new_str = unicodedata.normalize("NFKD", str_hard_space)
print(new_str)
if xa in new_str:
print("xa0 is Found!")
else:
print("xa0 is not Found!")
輸出:
17 kg on 23rd June 2021
xa0 is Found!
17 kg on 23rd June 2021
xa0 is not Found!
使用字串的 replace() 函式從 Python 中的字串中刪除 \xa0
你可以使用字串的 replace() 函式從字串中刪除 \xa0。
replace() 函式的用法如下。
str_hard_space.replace(u"\xa0", u" ")
下面的例子說明了這一點。
str_hard_space = "16\xa0kg on 24th\xa0June 2021"
print(str_hard_space)
xa = u"\xa0"
if xa in str_hard_space:
print("xa0 Found!")
else:
print("xa0 not Found!")
new_str = str_hard_space.replace(u"\xa0", u" ")
print(new_str)
if xa in new_str:
print("xa0 Found!")
else:
print("xa0 not Found!")
輸出:
16 kg on 24th June 2021
xa0 Found!
16 kg on 24th June 2021
xa0 not Found!
使用 BeautifulSoup 庫的 get_text() 函式將 strip 設為 True 從 Python 中的字串中刪除 \xa0
你可以使用 BeautifulSoup 標準庫的 get_text() 函式和 strip 啟用為 True 從字串中刪除 \xa0。
get_text() 函式的用法如下。
clean_html = BeautifulSoup(input_html, "lxml").get_text(strip=True)
下面的例子說明了這一點。
from bs4 import BeautifulSoup
html = "This is a test message, Hello This is a test message, Hello\xa0here"
print(html)
clean_text = BeautifulSoup(html, "lxml").get_text(strip=True)
print(clean_text)
輸出:
Hello, This is a test message, Welcome to this website!
Hello, This is a test message, Welcome to this website!
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe