Your solution function censor_four strips out the newlines from the original email when it creates a list with all the words:
def censor_four(input_text, censored_list):
for x in input_text.split(" “):
x1 = x.split(”\n")
for word in x1:
I was trying to figure out how to preserve the newlines in the word list before getting to the censoring code, so that when the text is re-joined after the censoring, it preserves the format of the original email with the newlines . I created two functions, the first to create a list with the email text split up by spaces, same as you, then a second function to parse that list and check each element for newlines (’\n’), and create a new list with the newlines as separate elements:
text_list = text.split(" ")
for x in range(len(text_list)):
if ‘\n’ in text_list[x ]
i = text_list[x ].find(’\n’)
I started with looking for one newline per element, and was going to expand on that code to find a second new line. But I found that even though elements in the first list have strings that correctly have the two newlines from email_four, for example ‘HELP!\n\nHelena’, when run through my separate_newlines function it appears to treat the first ‘\n’ as an escape sequence, and my new list has three elements ‘HELP’, ‘\n’,’ Helena’, rather than ‘HELP’, ‘\n’, ‘\nHelena’.
Is that to be expected, and is there a way to correctly recognize the first ‘\n’ as a newline and not an escape sequence?