Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
Set Error While Attempting to remove punctuation
-
I am attempting to write a program, within which I need to load a file, and remove all punctuation from it. In attempting to do so, I receive a 'set' object does not support indexing error. I am not sure which variable is a set, and why this is failing. A simplified version of my code is below
import string input_file = open("Test1.txt", "r") #Open the file 'Test1' input_file = input_file.read() #Convert the file to a string input_file = input_file.translate({None for a in string.punctuation}) #Attempt to remove punctuation. #The error is thrown by the above line
-
import string with open("Test1.txt", "r") as in_file: s = in_file.read() print(s) s = ''.join(' ' if c in string.punctuation else c for c in s) print(s)
-
{None for ....} is a set.
translate takes a dictionary where keys are unicode ordinals, and values are strings.
The translate you meant to use was:
translate({ord(x):None for x in string.punctuation})
-
Thank you for all of the responses