简体   繁体   English

Python:'NoneType'类型的对象没有len()

[英]Python: Object of type 'NoneType' has no len()

I running into bit of an issue and I'm hoping the stackoverflow crew would be able to help. 我遇到了一个问题,我希望stackoverflow工作人员能够提供帮助。

I keep getting the error message Object of type 'NoneType' has no len() whenever I attempt to separate a class of documents. 我不断收到错误消息每当我尝试分离一类文档时,类型为'NoneType'的对象都没有len()

The full trace-back is: 完整的追溯是:

C:\>C:\Python27\python.exe C:\Testing\test.py "C:\Testing\IN" "C:\Testing\Outputs" "C:\Testing\Test.csv"

C:\Testing\IN\000001.000001.xml

Traceback (most recent call last):
  File "C:\Testing\test.py", line 46, in <module>
    if len(documentclass)==0:
TypeError: object of type 'NoneType' has no len()

C:\>

Here's the code: 这是代码:

import csv, sys, os
import shutil
import xml.etree.ElementTree as ET


if __name__ == '__main__':
    if not (len(sys.argv) == 4):
        print 'USAGE: %s inFolder OutFolder csvFile' % (sys.argv[0])
    else:        
        inFolder    = sys.argv[1]
        outFolder   = sys.argv[2]
        className   = sys.argv[3]

        count = 0        
        for fileName in os.listdir(inFolder):
            if fileName.endswith(".pdf"):                
                baseName = fileName.split('.pdf')[0]
                pdfFile = inFolder+"\\"+baseName+".pdf"
                xmlFile = inFolder+"\\"+baseName+".xml"
                validatedXmlFile = inFolder+"\\"+baseName+".xml.validated.xml"                                
                xmlSize = os.path.getsize(xmlFile)
                pdfSize = os.path.getsize(pdfFile)                
                if xmlSize>0 and pdfSize>0:
                    print
                    print xmlFile
                    count = count + 1
                    tree = ET.parse(xmlFile)
                    root_xml = tree.getroot()
                    form_xml = root_xml[0]
                    #form_xml = root_xml[1]
                    documentclass_xml = form_xml.find('DocumentClassGlobal')                    
                    documentclassLocal_xml = form_xml.find('DocumentClassLocal')                
                    #documentclass_xml = form_xml.find('SSMClassID')
                    if documentclass_xml is not None:
                        documentclass = documentclass_xml.find('data').text                       
                    elif documentclassLocal_xml is not None:
                        documentclass = documentclassLocal_xml.find('data').text
                        documentclass = documentclass + "_Local"
                    else:
                        documentclass = ""                        
                    if len(documentclass)==0:
                        documentclass = "UNKNOWN"                
                    print documentclass                 

                    if documentclass == className:                                    
                        if not os.path.exists(outFolder + "\\" + documentclass):
                            os.makedirs(outFolder + "\\" + documentclass)
                        inBaseFile = inFolder + "\\"+baseName
                        outBaseFile = outFolder + "\\" + documentclass+"\\"+baseName                                
                        inFile = inBaseFile+".pdf"
                        outFile = outBaseFile+".pdf"
                        print inFile
                        print outFile
                        shutil.copy(inBaseFile+".pdf", outBaseFile+".pdf")
                        shutil.copy(inBaseFile+".pdf.conf.xml", outBaseFile+".pdf.conf.xml")
                        shutil.copy(inBaseFile+".pdf.multi.txt", outBaseFile+".pdf.multi.txt")
                        shutil.copy(inBaseFile+".pdf.txt", outBaseFile+".pdf.txt")
                        #shutil.move(inBaseFile+".wdb", outBaseFile+".wdb")
                        shutil.copy(inBaseFile+".xml", outBaseFile+".xml")
                        if os.path.exists(inBaseFile+".xml.validated.xml"):
                            shutil.copy(inBaseFile+".xml.validated.xml", outBaseFile+".xml.validated.xml")
                        if os.path.exists(inBaseFile+".xml.validationinfo.xml"):
                            shutil.copy(inBaseFile+".xml.validationinfo.xml", outBaseFile+".xml.validationinfo.xml")


        print '%d files found and copied.' % (count)

Obviously, the if len(documentclass)== 0 is returning the None value. 显然, if len(documentclass)== 0返回None值。 The idea is to assign the None value and the 0 value to documentclass - "Unknown" 我们的想法是将None值和0值分配给documentclass - “Unknown”

Thus far, I have come up with the below, but with no success. 到目前为止,我已经提出了以下内容,但没有成功。 Any ideas? 有任何想法吗?

Many Thanks 非常感谢

if documentclass_xml is not None:
                        documentclass = documentclass_xml.find('data').text                       
                    elif documentclassLocal_xml is not None:
                        documentclass = documentclassLocal_xml.find('data').text
                        documentclass = documentclass + "_Local"
if documentclass_xml is None:
                        documentclass = "UNKNOWN"                      
                    elif documentclassLocal_xml is None:
                        documentclass = "UNKNOWN"
                    else:
                        documentclass = ""                        
                    if len(documentclass)==0:
                        documentclass = "UNKNOWN"                
                    print documentclass

The traceback tells you that documentclass has the value None . 回溯告诉您documentclass的值为None You initialized it with: 你初始化它:

                if documentclass_xml is not None:
                    documentclass = documentclass_xml.find('data').text                       
                elif documentclassLocal_xml is not None:
                    documentclass = documentclassLocal_xml.find('data').text
                    documentclass = documentclass + "_Local"
                else:
                    documentclass = ""   

so at least one branch of the if must assign None to it. 所以if至少一个分支必须为它分配None Accessing the text attribute of an ElementTree node will return None if there is no text content in the node. 如果节点中没有文本内容,则访问ElementTree节点的text属性将返回None It has to be the first branch of the condition otherwise the attempt to append "_Local" would throw the error. 它必须是条件的第一个分支,否则尝试附加"_Local"会抛出错误。

>>> data = ET.fromstring('<test/>')
>>> data.text is None
True

Therefore you are accessing an empty <data/> node. 因此,您正在访问一个空的<data/>节点。

Turns out there were some changes made to DocumentClassGlobal and DocumentClassLocal. 事实证明,对DocumentClassGlobal和DocumentClassLocal进行了一些更改。 Before, these objects were created only if there was a value in documentclass_xml.find('data').text. 之前,只有在documentclass_xml.find('data')。text中有值时才会创建这些对象。 Now, this rule is ignored and documentclass_xml.find('data').text can be a None value. 现在,此规则被忽略,documentclass_xml.find('data')。text可以是None值。 I made the below adjustment and it did the trick. 我进行了以下调整,它完成了诀窍。 Thanks mgilson for pointing it out. 谢谢mgilson指出来。

if documentclass_xml is not None and documentclass_xml.find('data').text is not None:
                        documentclass = documentclass_xml.find('data').text                       
                    elif documentclassLocal_xml is not None and documentclass_xml.find('data').text is not None:
                        documentclass = documentclassLocal_xml.find('data').text
                        documentclass = documentclass + "_Local"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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