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 유니코드는 프로그램의 하드 공간 또는 중단 없는 공간을 나타냅니다. 로 표시됩니다. HTML에서.
문자열에서 \xa0을 제거하는 데 도움이 되는 Python 함수는 다음과 같습니다.
unicodedata의normalize()함수- 문자열의
replace()함수 strip이True로 활성화된BeautifulSoup라이브러리의get_text()함수.
Unicodedata의 Normalize() 함수를 사용하여 Python의 문자열에서 \xa0 제거
unicodedata 표준 라이브러리의 normalize() 함수를 사용하여 문자열에서 \xa0을 제거할 수 있습니다.
normalize() 함수는 다음과 같이 사용됩니다.
unicodedata.normalize("NFKD", string_to_normalize)
여기서 NFKD는 정규형 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 제거
strip을 True로 활성화하여 BeautifulSoup 표준 라이브러리의 get_text() 함수를 사용하여 문자열에서 \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!
튜토리얼이 마음에 드시나요? DelftStack을 구독하세요 YouTube에서 저희가 더 많은 고품질 비디오 가이드를 제작할 수 있도록 지원해주세요. 구독하다