Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

argument of type 'NoneType' is not iterable

I am trying to open a directory that contains a series of XML's in one specific directory.
In the following code, I am iterating through each XML document, and I'm setting some "if statements" to read the text in the XML, find keywords and replace them and then write a new file to a new location.
I am getting the following error when I run the script:

Traceback info:
  File "Z:\ESRI\Python\Test Scripts\ElementTree6.py", line 62, in <module>
    if "%begdate%" in element.text:
        ...

Error Info:

argument of type 'NoneType' is not iterable

I have hard coded the directory to one specific XML and when I run through the if statements, they work fine.
It's when I try to set up to iterate through a series of XML's that I run into the error.
I searched through this site to see if I could final a solution, but all the issues are either different from mine or I am not quite understanding the work-around.

I have used a number of print lines to test for outputs.
Everything works fine until I get to the if statement and then the error arises.

# Location of XML's
folderPath = r"Z:\data"

# set variable to store files with extension ".xml"
for filename in glob.glob(os.path.join(folderPath, "*.xml")):

fullpath = os.path.join(folderPath, filename)

# find files and split the filename from the directory path
if os.path.isfile(fullpath):
    basename, filename2 = os.path.split(fullpath)
    #print "Basename = " + basename
    #print "Filename = " + filename2

    # set variable to store XML structure from xml file
    root = ElementTree(file=r"Z:\data\\" + filename2)


    #Create an iterator
    iter = root.getiterator()
    #Iterate
    for element in iter:
        #print element.text

            if "%begdate%" in element.text:
            BEGDATE = element.text.replace("%begdate%", BEGDATEPARAM)
            element.text = BEGDATE
like image 475
Mike Avatar asked Jul 20 '11 22:07

Mike


2 Answers

You will find that there are many elements in an xml document which contain no character data (i.e. text). One of the nice things about python is that None is false in a conditional, coupling that with knowledge of how conditional statements short circuit means that there's a really simple solution to your problem, change the line:

if "%begdate%" in element.text:

to:

if element.text and "%begdate%" in element.text:

when there is no text, then element.text is None, a.k.a False, and then you won't even get to the part of the conditional which is causing the error.

like image 65
Hoons Avatar answered Oct 17 '22 01:10

Hoons


The error you gave would suggest that one of the tags you are iterating over contains no text.
I think a better way to do that check would be to replace that if statement with one of the following:

if "%begdate%" == element.text:
    #code goes here...

import re
if re.search("%begdate%", element.text):
    #code goes here...

The first example checks for equality, the second uses a regular expression to see if the tag contains your string. I'm not sure how regular expressions handle a None search space... that one might fail.

like image 21
rjacks Avatar answered Oct 17 '22 01:10

rjacks