【发布时间】:2012-04-12 10:13:39
【问题描述】:
我在解析一些 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 还是为我的print 使用不同类型的.encode?
顺便说一句,我从输入文件创建数据库实际上是将多个 XML 文件附加到一个文件。 (数据文件来源于Google Patents)。
【问题讨论】:
标签: python string lxml encode nonetype