Forum Archive

Set Error While Attempting to remove punctuation

Buzzerb

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
ccc
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)
JonB

{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})
Buzzerb

Thank you for all of the responses