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)ますか?

4

3 に答える 3

7

文字列から句読点を削除するには、次を使用します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'
于 2013-03-17T02:47:23.680 に答える
1

このstackoverflowの記事に示されている手法を使用して、句読点の削除を劇的に改善できます。次に、s.lower()を使用して文字列sを小文字にします。

于 2013-03-17T02:48:20.950 に答える