试图去除错误“TypeError:invalid File:[]”

时间:2014-02-25 03:34:38

标签: python string token punctuation

我试图通过从一个空数组开始然后将清理的标记添加到该数组来清除以下标点符号的一些标记。

def main():
    tokens = readTokens("words.txt")
    depunctuatuate = depunctuateToken(tokens)

def readTokens(fileName):
    s = Scanner(fileName)
    items = []
    token = s.readstring()
    while (token != ""):
        items.append(token)
        token = s.readstring()
    print(items)
    s.close()
    return items

def depunctuateToken(string):
    result = []
    s = Scanner(string)
    token = s.readstring()
    punct = [",",".","?",":","'","-"]
    for i in range(0,len(string),1):
        if(not(string[i] in punct)):
            result += [string[i]]
    return result

1 个答案:

答案 0 :(得分:0)

Scanner函数中不需要depunctuateToken

您正在传递一个字符串数组,只需遍历它们并对每个字符串进行punct过滤。您可能还想修改return语句,现在它看起来只返回第一个项目。