I am workign with a xml file that contains the order information for a company. What i am tryign to do is see if the xml file have a scan code in it and if it does then to pull the order number so that it can be passed to another function. I have a base start but I keep getting the error AttributeError: 'NoneType' object has no attribute 'text' and dont know how to fix it. Could someone explain to me what im doign wrong and how to fix my code.

tree = ET.ElementTree(file=args.destDir + '/' + 'FACTSentered'+args.fileDate+'.xml')
root=tree.getroot()
codeList = root.findall(".//ScanCodes/ScanCode/Code")
code_stats = {}
for el in codeList:
    #order = el.find('OrderNumber').text
    code = el.find('Code').text
    user = el.find('User').text
    if user is None:
        user = '??'
    seq = el.find('SeqNumber').text
    #print order
    print code
    print user
    print seq

What line is causing the error. Perhaps you could post the error message?

It sounds like one of your el.find statements is coming up empty (None) and that's causing the error

its saying all three of the el.find statements are throwing the error i commented out each of them individually and they all throw the smae error

codeList = root.findall(".//ScanCodes/ScanCode/Code")
print codeList

What dos "print codelist" output.
So here as a demo i use find() to find "value" and "text"

in [1]:    
xml = '''\
    <foo>
       <bar key="value">text</bar>
    </foo>'''

In [2]:    
import xml.etree.ElementTree as ET

In [3]:    
xml = ET.fromstring(xml)

In [4]:    
xml.find('./bar').attrib['key']   
Out[4]:
'value'

In [5]:
xml.find('./bar').text    
Out[5]:
'text'

Edited 2 Years Ago by snippsat

the pring codelist prints out the cancodes/scancode/code section to show that it has fount something. Right now its there for debugging purposes.

the pring codelist prints out the cancodes/scancode/code section to show that it has fount something.

Is it in "xml" foramt?
Post a sample.

If i iterate over codelist(for el in codeList:),
like you do i can not use find() anymore.

In [25]:

for el in xml:
    print type(el)
    print el.attrib['key']
    print el.text        

<class 'xml.etree.ElementTree.Element'>
value
text

this is the best i can give for a example had to remove all sensitive information from it and i jsut tried what you have showed with jsut printing the el.text and it didnt pring nothing

<ResponseBatch ConsumerKey="" Language="" DateTime="" SerialID="">
    <Response RequestID="" Company="" SerialID="">
        <Orders>
            <Order>
                <Order>
                    <OrderHeader>
                        <CompanyNumber></CompanyNumber>
                        <CustomerNumber></CustomerNumber>
                        <OrderNumber></OrderNumber>
                        <WebID></WebID>
                        <TypeOrder></TypeOrder>
                        <OrderSuffix></OrderSuffix>
                        <OrderType></OrderType>
                        <OrderStatus></OrderStatus>
                        <CustomerName/><CustomerAddress1/>
                        <CustomerAddress2/>
                        <BillToCity/><BillToState/>
                        <BillToZipCode/><BillToContact/>
                        <ShipToNumber/><ShipToName/>
                        <ShipToAddress1/><ShipToAddress2/>
                        <ShipToCity/><ShipToState/>
                        <ShipToZipCode/>
                        <Contact></Contact>
                        <EntryDate></EntryDate>
                        <RequestedShipDate></RequestedShipDate>
                        <CustomerPurchaseOrderNumber></CustomerPurchaseOrderNumber>
                        <ItemSalesAmount></ItemSalesAmount>
                        <DiscountAmountTrading></DiscountAmountTrading>
                        <SalesTaxAmount></SalesTaxAmount>
                        <InvoiceAmount></InvoiceAmount>
                        <TotalOrderValue></TotalOrderValue>
                        <TotalSpecialCharges></TotalSpecialCharges>
                        <CarrierCode></CarrierCode>
                        <WarehouseID></WarehouseID>
                        <InvoiceNumber/>
                        <ShipTrackingReference/>
                    </OrderHeader>
                    <ScanCodes>
                        <ScanCode>
                            <Code></Code>
                            <User></User>
                            <DateTime></DateTime>
                            <Comment/>
                            <LineNumber></LineNumber>
                            <SeqNumber></SeqNumber>
                        </ScanCode>
In [34]:    
codeList = '''\
<ScanCode>
    <Code>Superman 12345</Code>
    <User>Clark kent</User>
    <DateTime></DateTime>
    <Comment/>
    <LineNumber></LineNumber>
    <SeqNumber></SeqNumber>
</ScanCode>'''

In [35]:    
import xml.etree.ElementTree as ET

In [37]:    
xml = ET.fromstring(codeList)

In [41]:    
xml.find('User').text
Out[41]:
'Clark kent'

In [42]:    
xml.find('Code').text
Out[42]:
'Superman 12345'

Edited 2 Years Ago by snippsat

I figured out how to get what i was looking for now i have hit another snag that i will have to post another question about. But the answer was to use

for el in codeList:
    code = el.find('Code').text
This question has already been answered. Start a new discussion instead.