텍스트 정리기
텍스트에서 불필요한 공백, 빈 줄, 특수 문자, HTML 태그를 제거합니다.
텍스트 정리기는 한 번의 클릭으로 텍스트에서 원하지 않는 서식, 여분의 공백, 일반적인 잡동사니를 제거합니다. PDF, Word 문서, 웹 페이지, 이메일에서 텍스트를 복사하면 종종 여분의 줄바꿈, 연속 공백, 보이지 않는 유니코드 문자, 코드를 깨는 스마트 따옴표, &나 같은 HTML 엔티티가 포함됩니다.
필요한 정리 작업을 선택하고 정리 버튼을 클릭하세요. 사용 가능한 작업에는 여분의 공백 및 빈 줄 제거, 앞뒤 공백 제거, 여러 공백을 하나로 축소, 스마트/곡선 따옴표를 직선 따옴표로 변환, HTML 태그 제거, HTML 엔티티 디코딩, 출력 불가 문자 제거 등이 포함됩니다.
텍스트 정리는 데이터 처리 파이프라인의 일반적인 첫 단계입니다. 텍스트 데이터를 데이터베이스, 머신러닝 모델, API에 가져올 때 예상치 못한 공백과 특수 문자는 파싱 오류의 빈번한 원인입니다.
자주 묻는 질문
코드 구현
import re
import unicodedata
def remove_control_chars(text: str) -> str:
"""Remove non-printable control characters (keep tab, newline, carriage return)."""
return "".join(
ch for ch in text
if unicodedata.category(ch) not in ("Cc", "Cf") or ch in ("\t", "\n", "\r")
)
def normalize_line_endings(text: str, style: str = "lf") -> str:
"""Normalize line endings to LF (Unix) or CRLF (Windows)."""
text = text.replace("\r\n", "\n").replace("\r", "\n")
if style == "crlf":
text = text.replace("\n", "\r\n")
return text
def collapse_whitespace(text: str) -> str:
"""Replace multiple consecutive spaces/tabs on each line with a single space."""
return "\n".join(re.sub(r"[ \t]+", " ", line) for line in text.splitlines())
def trim_lines(text: str) -> str:
"""Strip leading and trailing whitespace from each line."""
return "\n".join(line.strip() for line in text.splitlines())
def remove_blank_lines(text: str) -> str:
"""Collapse multiple consecutive blank lines into one."""
return re.sub(r"\n{3,}", "\n\n", text)
def clean_text(text: str,
control_chars: bool = True,
normalize_endings: bool = True,
collapse_spaces: bool = True,
trim: bool = True,
blank_lines: bool = True) -> str:
"""Run all cleaning steps in sequence."""
if control_chars:
text = remove_control_chars(text)
if normalize_endings:
text = normalize_line_endings(text)
if collapse_spaces:
text = collapse_whitespace(text)
if trim:
text = trim_lines(text)
if blank_lines:
text = remove_blank_lines(text)
return text
sample = " Hello\t world! \n\n\nExtra blank lines \n\x00Null byte here "
print(repr(clean_text(sample)))
# 'Hello world!\n\nExtra blank lines\nNull byte here'Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.