Python 3 Omit Characters - Naive methods

Viewed 146

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?

2 Answers

When the character isn't found in punctuation_list, you need to add it to punct_omit. Otherwise, all you get are the spaces, not the ordinary characters.

You don't need to call replace(), just append a space to punct_omit.

Also, you should be iterating over the text, not punctuation_string.

def omit_characters(text):
    punctuation_string = ",.!?;:][-\""
    punct_omit = ""
    for mark in text: 
        if mark in puctuation_string:
            punct_omit += " "
        else:
            punct_omit += mark
    print(punct_omit)

omit_characters(alice_text)

You can also do it with replace. Then you need to use the previous replaced string as the input on each iteration:

def omit_characters(text):
    punctuation_string = ",.!?;:][-\""
    for mark in punctuation_string:
        text = text.replace(mark, " ")
    print(text)

Something like the following could work if you only use ascii characters. It replaces the characterset. A character is basically just a number (0-128), which can be displayed in any way (the characterset), so replacing the characterset with your variation would solve the issue.
You'd need to replace the not-allowed characters in the keyspace with \x20 to get a " ".

def int_to_charset(val, charset):
    output = ""
    while val > 0:
        val, digit = divmod(val, len(charset))
        output += charset[digit]
    # reverse the characters in the output and return
    return output[::-1]


def charset_to_int(s, charset):
    output = 0
    for char in s:
        output = output * len(charset) + charset.index(char)
    return output


def change_charset(s, original_charset, target_charset):
    intermediate_integer = charset_to_int(s, original_charset)
    return int_to_charset(intermediate_integer, target_charset)


origspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\x20\x20#$%&\'()*+\x20-\x20/:;\x20@[\\]^_`{|}~ \t\n\r\x0b\x0c'

text = "Hi! Is my name Alice? I can\"t be too sure about that!"

print(change_charset(text, origspace, keyspace))
Related