문제

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).

다른 팁

To support punctuation inside the strings:

import ast
import fileinput

for line in fileinput.input(inplace=1): #NOTE: replace inplace
    print " ".join(ast.literal_eval(line))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top