Reputation: 1
I need to tie in the punctuation function so text in a file is printed without punctuation. Take a line: "How are you today?"
So far that prints:
"how
are
you
today?"
But I want to print it like:
how
are
you
today
My code looks like this:
from scanner import *
import sys
import string
def processFile(filename):
s = Scanner(filename)
token = s.readtoken()
array = []
while token != "":
newToken = ""
for i in range(0,len(token),1):
newchar = RawChar(token[i])
newToken = newToken + newchar
array.append(newToken)
token = s.readtoken()
s.close()
return array
def eachLine(tokens):
for i in range(0,len(tokens),1):
pun(tokens[i])
print(tokens[i])
return
def pun(string):
punctuation = ["`","~","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]","|",":",";","\"","'","<",",",">",".","?","/"]
for i in string:
newString = ""
if i not in string:
newString = newString + i
return newString
def RawChar(char):
if char == "A":
char = "a"
elif char == "B":
char = "b"
elif char == "C":
char = "c"
elif char == "D":
char = "d"
elif char == "E":
char = "e"
elif char == "F":
char = "f"
elif char == "G":
char = "g"
elif char == "H":
char = "h"
elif char == "I":
char = "i"
elif char == "J":
char = "j"
elif char == "K":
char = "k"
elif char == "L":
char = "l"
elif char == "M":
char = "m"
elif char == "N":
char = "n"
elif char == "O":
char = "o"
elif char == "P":
char = "p"
elif char == "Q":
char = "q"
elif char == "R":
char = "r"
elif char == "S":
char = "s"
elif char == "T":
char = "t"
elif char == "U":
char = "u"
elif char == "V":
char = "v"
elif char == "W":
char = "w"
elif char == "X":
char = "x"
elif char == "Y":
char = "y"
elif char == "Z":
char = "z"
return char
def main():
newForm = processFile(sys.argv[1])
eachLine(newForm)
main()
Any suggestions as where to put the def pun(string)
?
Upvotes: 0
Views: 764
Reputation: 122022
import string
s = '"Right now!" she shouted, and hands fluttered in the air - amid a few cheers - for about two minutes.'
x = "".join([c for c in s if or c not in string.punctuation])
Upvotes: 0
Reputation: 78583
You can dramatically improve the punctuation stripping using techniques shown in this stackoverflow article. And then use s.lower() to lowercase string s.
Upvotes: 1
Reputation: 879381
To remove punctuation from a string, use str.translate
:
In [124]: import string
In [126]: string.punctuation
Out[126]: '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
In [127]: '"How are you today?"'.translate(None, string.punctuation)
Out[127]: 'How are you today'
Upvotes: 7