0

I am trying to learn how to convert xml to csv format and looked up some tutorials. I am doing the following, but getting an error: 'NoneType' object has no attribute 'text''

So the part XML file is the following (it is actually bigger, but I reduced it for the sake of example):


<?xml version="1.0" encoding="UTF-8" standalone="true"?>

-<ns2:export xmlns:ns2="http://zakupki.gov.ru/oos/export/1" xmlns="http://zakupki.gov.ru/oos/types/1">

-<ns2:fcsNotificationZK schemeVersion="1.0">

<id>27778</id>

<purchaseNumber>0373100113714000006</purchaseNumber>

<docPublishDate>2014-01-24T18:40:51.599+04:00</docPublishDate>

And then I am writing the following code to get the tag and the information for the tag:

import csv
import xml.etree.cElementTree as ET

tree = ET.parse("xml_notification_example.xml")
root = tree.getroot()

xml_data_to_csv = open('out.csv', 'w')
list_head = []
csv_writer = csv.writer(xml_data_to_csv)

count = 0

for element in root:
    list_nodes = []
    
    if count == 0:
        root.findall('id')
        list_head.append('id')

        root.findall('purchaseNumber')
        list_head.append('purchaseNumber')

        root.findall('docPublishDate')
        list_head.append('docPublishDate')
        
        csv_writer.writerow(list_head)
        count = +1
    
    iden = element.find('id').text
    list_nodes.append(iden)
    
    purchaseNumber = element.find('purchaseNumber').text
    list_nodes.append(purchaseNumber)
       
    docPublishDate = element.find('docPublishDate').text
    list_nodes.append(docPublishDate)
    
    csv_writer.writerow(list_nodes)

xml_data_to_csv.close()

And I get the mistake:

---> 19     iden = element.find('id').text
     20     list_nodes.append(iden)
     21 

AttributeError: 'NoneType' object has no attribute 'text'

Can you,please, give me a clue what is wrong here, even intuitively?

1 Answer 1

1

The xml contains a namespace xmlns="http://zakupki.gov.ru/oos/types/1". Therefore this needs to be referenced in the find().

Try:

element.find('{http://zakupki.gov.ru/oos/types/1}id').text

Output:

'27778'

Hardcoding namespaces is messy & ugly. To get it dynamical, see the combined answers of @Rik Poggi and @Mark Ransom: Python: ElementTree, get the namespace string of an Element

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! I was able to do it this way!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.