AttributeError:'NoneType'对象没有带lxml-python的属性'encode'

时间:2012-03-28 18:58:26

标签: python string lxml encode nonetype

在解析某些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'

无论如何要么忽略没有任何东西时返回的“None”值?我必须def或为.encode使用其他类型的print吗?

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

1 个答案:

答案 0 :(得分:4)

你总是可以快速而肮脏str(inventor1.encode("UTF-8") if inventor1 else inventor1)