ComfyUI Node

Text Trim

Strip, collapse, and drop blank lines

By wwsmiao·Created 4 months ago·Updated 4 months ago· 0
Text Trim
    • text
    text
    modetrim
    chars

    LLM output and OCR text arrive filthy - leading spaces, trailing newlines, runs of blanks, empty lines in the middle. TextTrim is the first stop in the wwdm-aicd pack's cleaning pipeline (the README literally opens its text-cleaning example with it), and it has six modes that cover the different kinds of dirty.

    trim, ltrim, rtrim do what they say - strip whitespace (or, if you set chars, a specific set of characters) from both ends, the left, or the right. collapse is the one people discover and love: it squeezes every run of whitespace down to a single space. "Hello World" becomes "Hello World" - great for normalizing text that got mangled by line breaks or double spaces. strip_lines and remove_blank_lines work line-by-line on multi-line text: strip_lines trims each line and drops empty ones (result stays multi-line, joined with newlines), while remove_blank_lines only drops the empties and leaves other lines untouched. If you're cleaning a tag list or a multi-line prompt block, strip_lines is usually the one you want.

    The inputs are text, mode, and an optional chars. The chars detail is the subtle bit: when it's empty, stripping uses Python's default whitespace behavior; when you fill it in, only those characters get stripped. So chars: ".,!" on "...hello!..." gives you "hello" - punctuation peeled, spaces untouched. If you set chars expecting it to strip both whitespace and your characters, that's not how it works; it's an either/or.

    Mechanically it's Python's strip family plus a regex for collapse. One quirk: the collapse mode also strips the whole result, so leading/trailing whitespace disappears too - which is what you want 99% of the time but worth knowing.

    Install, standard for this pack:

    cd ComfyUI/custom_nodes
    git clone https://github.com/wwsmiao/comfyui-wwdm-aicd.git
    

    Restart ComfyUI, find it under wwdm-aicd. No dependencies beyond stdlib; the README's pip install -r requirements.txt is a non-step because there's no requirements.txt in the repo. The author's README and source comments are in Chinese; widget labels are English.

    Where it shines: right after AIChatAPI or any text-generation node, before the output goes to a parser or an encoder. Trim the junk, then TextReplace the symbols, TextCase the casing, TextLength to verify - that's the pack's own recommended chain, and it's a good one. It's a boring node and that's its best quality: it makes the messy text you actually get from real tools look like the clean text you assumed you'd have.

    Categorywwdm-aicd

    Inputs (3)

    NameTypeDefaultDescription
    textSTRING
    modeCOMBOtrim6 options: trim, ltrim, rtrim, collapse, strip_lines, remove_blank_lines
    charsoptSTRING

    Outputs (1)

    NameTypeDescription
    textSTRING