我正在使用这段代码并将csv文件作为列表获取到doc [].中。
doc = []
with open(r'C:\Users\DELL\Desktop\Final project\Requirements1.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
for riga in csv_reader:
for campo in riga:
print(campo)
doc.append(nlp(campo))但是,当我使用下面的代码对此进行命名实体识别时,
for entity in doc.ents:
print(entity.text, entity.label)我得到了这个错误.
AttributeError: 'list' object has no attribute 'ents',我该怎么办?请帮帮我。在这里输入图像描述
发布于 2021-06-19 04:34:01
这样做。
docs = [] # NOTE THIS CHANGED
with open(r'C:\Users\DELL\Desktop\Final project\Requirements1.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=';')
for riga in csv_reader:
for campo in riga:
print(campo)
docs.append(nlp(campo))
# now to get the ner results...
for doc in docs:
for ent in doc.ents:
print(ent.text, ent.label)发布于 2021-06-06 18:50:06
错误是不言自明的--您列出了一个名为"doc“的普通Python对象列表。Python列表没有名为"ents“的属性。
只需迭代列表中的元素,如下所示:
for entity in doc:
print(entity.text, entity.label)如果list元素确实具有属性'text‘和'label’,这应该有效(不能从代码中验证它们确实具有这些属性)
https://stackoverflow.com/questions/67862504
复制相似问题