🛠️ToolsShed

Pembersih Teks

Hapus spasi berlebih, baris kosong, karakter khusus, dan tag HTML dari teks.

Pembersih Teks menghapus format yang tidak diinginkan, spasi berlebih, dan sampah umum dari teks dengan satu klik. Saat Anda menyalin teks dari PDF, dokumen Word, halaman web, atau email, hasilnya sering mengandung jeda baris ekstra, beberapa spasi berurutan, karakter Unicode yang tidak terlihat, atau entitas HTML seperti & dan  .

Pilih operasi pembersihan yang Anda butuhkan dan klik Bersihkan. Operasi yang tersedia meliputi: menghapus spasi putih berlebih dan baris kosong, memangkas spasi di awal dan akhir, mengompres beberapa spasi menjadi satu, mengubah tanda kutip tipografis menjadi tanda kutip lurus, menghapus tag HTML, mendekode entitas HTML, dan menghapus karakter yang tidak dapat dicetak.

Pembersihan teks adalah langkah pertama yang umum dalam pipeline pemrosesan data. Saat mengimpor data teks ke database, model machine learning, atau API, spasi putih yang tidak terduga dan karakter khusus sering menjadi sumber kesalahan parsing.

Pertanyaan yang Sering Diajukan

Implementasi Kode

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.