[Python] Split a string into separate words without spaces, preserving special characters...

Discussão em 'Python' iniciado por Stack, Março 24, 2025.

  1. Stack

    Stack Membro Participativo

    My task:

    Is to split a string that does not have spaces into separate words. For example:

    "ProgramAgreement" -> "Program Agreement"

    "drivername" -> "driver name"

    For these purposes, I use the wordninja library, and in most cases I get an acceptable result.

    Problem:

    If the string contains special characters, wordninja ignores them, which is unacceptable for me. Here are some examples:

    'addressbelow:' -> 'address below'

    "PAGES#NUMBER" -> "PAGES NUMBER"

    Desire result:

    However, as you probably already guessed, I would like to preserve special characters. That is, I would like to see the following result:

    'addressbelow:' -> 'address below:'

    "PAGES#NUMBER" -> "PAGES# NUMBER"

    Code snippet:

    my code in this moment really simple

    import wordninja

    separate_words = " ".join(wordninja.split("PAGES#NUMBER"))

    Continue reading...

Compartilhe esta Página