AttributeError:"NoneType"对象没有带有lxml-python的属性"encode" [英] AttributeError: 'NoneType' object has no attribute 'encode' with lxml-python
问题描述
我得到 AttributeError:解析某些XML专利发明人数据时,'NoneType'对象没有属性'encode'
错误.我正在尝试将第一个发明人及其地址信息放入一个字符串中,如下所示:
I'm getting AttributeError: 'NoneType' object has no attribute 'encode'
error when parsing out some XML patent inventor data. I'm trying to pull the first inventor plus their address infomation into a string as such below:
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)
但是存在问题,因为存在一个 UnicodeEncodeError:'ascii'编解码器无法对此长xml中的多个部分进行字符编码
.我需要在python中包含 .encodes
,这样我就不会产生错误,但是这样做了:
but there is problem as there is a UnicodeEncodeError: 'ascii' codec can't encode character
for multiple parts in this long xml. I need to have the .encodes
within my python so I don't create an error but by doing so I get this:
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
?
Is there anyway to either ignore the "None" values that are returned when nothing is there? Must I def
or use a different type of .encode
for my print
?
通过我从输入文件创建数据库的方式实际上是将多个XML文件附加到一个文件中.(数据文件来自 Google专利).
By the way i'm creating a database from an input file is actually multiple XML files appending to one file. (Data file Sourced from Google Patents).
推荐答案
您总是可以快速而又肮脏地执行 str(inventor1.encode("UTF-8")(如果使用inventor1或inventor1)
You could always just do quick and dirty str(inventor1.encode("UTF-8") if inventor1 else inventor1)
这篇关于AttributeError:"NoneType"对象没有带有lxml-python的属性"encode"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!