I have a very lengthy text file which I opened and saved to a string variable of which I am looking to remove all special characters and replace it with a space. I am not allowed to import other libraries, so I have to keep it pretty naive. I figured running a for loop through my text file, comparing to see if the given punctuation is in the text, and replace it with a " ". I would then print the updated text string, for some reason (probably a simple fix for a more trained eye) I can't get it to remove the special characters. Per instructions, I need to remove the special characters first, before splitting the text file's string into a list of words.
alice_txt = "Hi! Is my name Alice? I can\"t be too sure about that!"
def omit_punctuation():
punct_string = ",.!?;:][-\""
punct_list = [",",".","!","?",";",":","]","[","-","\""]
punct_omit = ""
for mark in punct_list:
if mark in alice_txt:
punct_omit += alice_txt.replace(mark, " ")
print(punct_omit)
omit_characters()
Output:
Hi Is my name Alice? I can"t be too sure about that Hi! Is my name Alice I can"t be too sure about that!Hi! Is my name Alice? I can t be too sure about that!
Instead of the desired: Hi Is my name Alice I cant be too sure about that
I've tried the for loop with both punctuation_list as well as :_string. It looks like (as the code states) it's looking at each mark, and when it finds that mark, it does the replacement for each successive mark in the iteration, but then does not store the replacement for each next iteration. What am I not understanding in this method?