Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Preventing removal of linebreaks

I have a function that replaces offensive words with a star, but in running text through this, it strips out linebreaks. Any thoughts on how to prevent this?

def replace_words(text, exclude_list):
    words = text.split()
    for i in range(len(words)):
        if words[i].lower() in exclude_list:
            
            words[i] = "*"
    return ' '.join(words)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Don’t use .split() with no argument on the entire input string, it removes line breaks and you lose the information where you have to put them in the result string.

You could first split the input into lines and then process each line separately in the same way as you now process the whole input.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading