AttributeError: 'list' object has no attribute 'translate' while removing punctuation from text
AttributeError: 'list' object has no attribute 'translate' while removing 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 有类型
<class 'list'>
和以下表格:
[['hello', 'there', 'world!'], ['i', '"like"', 'potatoes.']]
应用函数后我想得到结果:
[['hello', 'there', 'world'], ['i', 'like', 'potatoes']]
好像是什么问题?
你有一个列表列表。
尝试:
train_pos_no_punctuation = [remove_punctuation(i) for i in train_pos]
我在尝试从文本中删除标点符号时遇到此错误。
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 有类型
<class 'list'>
和以下表格:
[['hello', 'there', 'world!'], ['i', '"like"', 'potatoes.']]
应用函数后我想得到结果:
[['hello', 'there', 'world'], ['i', 'like', 'potatoes']]
好像是什么问题?
你有一个列表列表。
尝试:
train_pos_no_punctuation = [remove_punctuation(i) for i in train_pos]