属性错误:'NoneType'对象没有'lxml-python'的编码属性。

2024-06-01 14:37:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我在解析一些XML专利发明人数据时遇到了AttributeError: 'NoneType' object has no attribute 'encode'错误。我正试图将第一个发明人加上他们的地址信息组成一个字符串,如下所示:

inventor1 = first(doc.xpath('//applicants/applicant/addressbook/last-name/text()'))
inventor2 = first(doc.xpath('//applicants/applicant/addressbook/first-name/text()'))
inventor3 = first(doc.xpath('//applicants/applicant/addressbook/address/city/text()'))
inventor4 = first(doc.xpath('//applicants/applicant/addressbook/address/state/text()'))
inventor5 = first(doc.xpath('//applicants/applicant/addressbook/address/country/text()'))
inventor = str(inventor2.encode("UTF-8")) + " " + str(inventor1.encode("UTF-8"))
inventors2 = str(inventor3.encode("UTF-8")) + ", " + str(inventor4) + ", " + str(inventor5)
inventors = str(inventor) + ", " + str(inventors2)

print "DocID: {0}\nGrantDate: {1}\nApplicationDate: {2}\nNumber of Claims: {3}\nExaminers: {4}\nAssignee: {5}\nInventor: {6}\n".format(docID,grantdate,applicationdate,claimsNum,examiners.encode("UTF-8"),assignees,inventors)

但是问题是,对于这个长xml中的多个部分有一个UnicodeEncodeError: 'ascii' codec can't encode character。我需要在python中使用.encodes,这样我就不会创建错误,但是通过这样做,我得到:

Traceback (most recent call last):
  File "C:\Documents and Settings\Desktop\FINAL BART INFO ONE.py", line 87, in <module> inventor = str(inventor2.encode("UTF-8")) + " " + str(inventor1.encode("UTF-8"))
AttributeError: 'NoneType' object has no attribute 'encode'

是否仍然可以忽略“无”值,这些值在没有任何内容时返回?我必须def或者使用不同类型的.encode来处理我的print

顺便说一下,我从一个输入文件创建数据库实际上是多个XML文件附加到一个文件。(数据文件来源于Google Patents)。


Tags: textdocaddressxmlxpathutfencodefirst