2013-03-17 101 views
0

我需要配合標點符號功能才能打印文件中的文本而不用標點符號。以一個線:"How are you today?"刪除標點符號

到目前爲止,打印:

"how 
are 
you 
today?" 

但我想打印出來,如:

how 
are 
you 
today 

我的代碼如下所示:

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

任何建議作爲在哪裏把def pun(string)

+0

你應該標記你的帖子,例如使用「蟒蛇」或其它相關的標籤,使合適的人看到你的帖子。此外,您應該使用內置工具正確設置您的帖子的格式,例如通過指示代碼片段以使其格式正確。 – jarmod 2013-03-17 02:41:54

+0

'如果我不在字符串中';你的意思是「如果我不在標點符號」,而不是? – Th3Cuber 2013-03-17 02:47:07

+2

花了一些時間閱讀關於'str'對象方法的文檔可能會有所收穫。例如,它看起來像你的'RawChar'函數只是'char.lower()'。 – DSM 2013-03-17 02:52:32

回答

7

從字符串刪除標點符號,使用str.translate

In [124]: import string 

In [126]: string.punctuation 
Out[126]: '!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~' 

In [127]: '"How are you today?"'.translate(None, string.punctuation) 
Out[127]: 'How are you today' 
0
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])