An argument of type "NoneType" is not repeated

I am trying to open a directory containing a series of XML in one specific directory.
In the following code, I repeat each XML document, and I set some “if statements” to read the text in XML, search for keywords and replace them, and then write a new file to a new location. When running the script:

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

Error Information:

argument of type 'NoneType' is not iterable

I hardcoded the directory for one specific XML, and when I run the if statements, they work fine.
This is when I try to set up an iteration using an XML series in which I encountered an error.
I looked through this site to find out if I can solve the final solution, but all the problems are either different from mine, or I don’t quite understand the work.

I used multiple print lines to check the output.
Everything works fine until I get to the if statement, and then an error occurs.

# 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
+5
source share
2 answers

, XML- , (.. ). python , None , , , , :

if "%begdate%" in element.text:

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

, element.text - None, a.k.a False, , .

+19

, , , . , - if :

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

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

, , , . , None... .

+1

All Articles