Python:使用translate方法从读入文本中删除标点符号

时间:2015-06-10 17:09:44

标签: python

我正在尝试打开并读取文件,将内容拆分为单词,然后遍历它们并删除所有标点符号。为什么这会返回TypeError: expected a character buffer object,我该如何解决?

with open(self.filename, "r") as file_opened:
    text = file_opened.read()
    words_with_punct = text.split()
    remove = dict.fromkeys(map(ord, '\n' + string.punctuation))
    for i in words_with_punct:
        words = i.translate(remove)
    return words

1 个答案:

答案 0 :(得分:0)

text = file_opened.read().translate(None,string.punctuation)

应该工作正常,不确定你所做的所有其他事情应该做什么