Search code examples
pythonattributeerror

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


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?


Solution

  • You have a list of lists.

    try:

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