AttributeError: 'list' object has no attribute 'translate' while removing punctuation from text

时间:2018-04-26 17:02:46

标签: python attributeerror

I'm getting this error while trying to remove punctuation from text.

train_pos_no_punctuation = remove_punctuation(train_pos)

def remove_punctuation(from_text):
    table = str.maketrans('', '', string.punctuation)
    stripped = [w.translate(table) for w in from_text]
    return stripped

train_pos has type

<class 'list'>

and the following form:

[['hello', 'there', 'world!'], ['i', '"like"', 'potatoes.']]

After applying the function I want to get the result:

[['hello', 'there', 'world'], ['i', 'like', 'potatoes']]

What seems to be the problem?

1 个答案:

答案 0 :(得分:1)

您有一份清单。

尝试:

train_pos_no_punctuation = [remove_punctuation(i) for i in train_pos]