How can I remove punctuation from a string in Python? -
i trying remove punctuation string whenever run program nothing happens... code:
#open file (a christmas carol) inputfile = open('h:\documents\computing\gcse computing\revision\practice prog/christmascarol.txt') caroltext = inputfile.read() #convert lowercase line in caroltext: caroltextlower = caroltext.lower() #remove punctuation (put space instead of hyphened word or apostrophe) import string exclude = set(string.punctuation) nopunctu = caroltextlower.join(ch ch in caroltextlower if ch not in exclude) print(nopunctu)
when run program, nothing appears
here's repaired version of code.
import string #open file (a christmas carol) inputfile = open(r'h:\documents\computing\gcse computing\revision\practice prog/christmascarol.txt') caroltext = inputfile.read() inputfile.close() #convert lowercase caroltextlower = caroltext.lower() #remove punctuation exclude = set(string.punctuation) nopunctu = ''.join(ch ch in caroltextlower if ch not in exclude) print(nopunctu)
the usual python convention put import
statements @ top of script they're easy find.
note used raw string (indicated r
before opening quote mark) file name. it's not strictly necessary here, prevents backslash sequences in windows paths being interpreted escape sequences. eg in 'h:\documents\new\test.py'
\n
interpreted newline character , \t
interpreted tab character.
you should close file after you've finished reading (or writing) it. however, it's better use with
keyword open files: ensures file gets closed if there's error. eg,
filename = r'h:\documents\computing\gcse computing\revision\practice prog/christmascarol.txt' open(filename) inputfile: caroltext = inputfile.read()
Comments
Post a Comment