I have an input file, each line of which is in the formar of a list in python. It looks something like this:
['people', 'desert', 'snow']
['people', 'flower', 'garden', 'goat']
I want to process this file and remove all the punctuations from it, i.e. "[", "]", "," and "'"
I am using the following code:
import string
import re
openfile=open('jcn','r')
writefile=open('jcnout','w')
punctuation=["[","]",",","'"]
for line in openfile:
line.translate(None, string.punctuation)
writefile.write(line)
writefile.flush()
writefile.close()
openfile.close()
But it doesnt seem to work, i.e. punctuation are retaind in the output file. Could someone please tell me where i am wrong
You need to change
line.translate(None, string.punctuation)
to
line = line.translate(None, string.punctuation)
In Python, strings are immutable. Correspondingly, translate()
doesn't change the string in place, but rather returns the translated string (which you're ignoring).