簡體   English   中英

AttributeError: 'list' object 沒有屬性 'ents'

[英]AttributeError: 'list' object has no attribute 'ents'

我正在使用此代碼並將 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'

我該怎么辦? 請幫我。 在此處輸入圖像描述

該錯誤是不言自明的 - 您制作了一個名為“doc”的普通 Python 對象列表。 Python 列表沒有名為“ents”的屬性。

只需像這樣遍歷列表中的元素:

for entity in doc:
    print(entity.text, entity.label)

如果您的列表元素確實具有屬性“文本”和“標簽”,這應該可以工作(無法從顯示的代碼中驗證它們確實具有這些屬性)

做這個。

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM