0

I am working on xml file of 210mb size using python. The data contain Nonetype and it give error as "TypeError: 'NoneType' object is not iterable"

import xml.etree.ElementTree as etree
nsmap = {}
lis = []
for event, elem in etree.iterparse("data_1.xml", events=('start-ns', 'end-ns'), remove_blank_text=True):
    try:
        print event, elem
        ns, url = elem
    #filter(None, lis)
    except ValueError:
        nsmap[ns] = url
print nsmap


TypeError                                 Traceback (most recent call last)
<ipython-input-23-23b2c0b682e8> in <module>()
      4     try:
      5         print event, elem
----> 6         ns, url = elem
      7     #filter(None, lis)
      8     except ValueError:

TypeError: 'NoneType' object is not iterable

Don't how to solve it. I just want to print the data. Even without using try and except method it show same error.

2 Answers 2

3

The exception you catch is of TypeError and not ValueError. If you want to catch the exception, replace ValueError by TypeError or leave it blank (you will henceforth catch all exceptions). Note that you should not try to use ns or url in the except block since they are undefined.

The other solution to do is:

if elem is not None:
    ns, url = elem
    nsmap[ns] = url
Sign up to request clarification or add additional context in comments.

2 Comments

or use a tuple except (ValueError, TypeError) as e: ...
Yes indeed. However, I don't see how it may raise a ValueError actually (perhaps I am missing a trivial case).
1

Add a check for None

if elem is None:
    continue

Or do whatever you want to handle elem being None inside of the block

Comments

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.