简体   繁体   中英

AttributeError: 'list' object has no attribute 'ents'

I am using this code and get the csv file as a list into 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))

But, When I do the named entity recognition for this using this code below,

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

I am getting this error.

AttributeError: 'list' object has no attribute 'ents'

What should I do about this? Please help me. enter image description here

The error is self explanatory - you made an ordinary Python list of objects called "doc". A Python list has no attribute called "ents".

Simply iterate through the elements in the list like this:

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

Provided your list elements indeed have attributes 'text' and 'label' this should work (can't verify from the code shown that they do have those attributes)

Do this.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM