-5

During using the below code which is available in here, the syntax error will occur to me and I don't know why! I guessed it's because I didn't install the mentioned library in code, but it isn't.

import os
import xml.etree.ElementTree as ET
#A helpful function to load compressed or uncompressed XML files
def loadXMLFile("config.xml"):
    #Check if the file is compressed or not, and
    if (os.path.splitext("config.xml")[1][1:].strip() == "bz2"):
        import bz2
        f = bz2.BZ2File("output.bz2")
        doc = ET.parse(f)
        f.close()
        return doc
else:
        return ET.parse("config.xml")

#Load the XML file config.out.xml
XMLDoc=loadXMLFile("config.out.xml")

#We can create a list of all particle tags using an xpath expression
#(xpath expressions always return lists)
PtTags = XMLDoc.findall("//Pt")

#Print the number of particles
print len(PtTags)

#print the x, y, and z positions of each particle
for PtElement in PtTags:
    PosTag = PtElement.find("P")
    print PosTag.get("x"), PosTag.get("y"), PosTag.get("z"), PtElement.get("D")

Here is the orginal file in which there is "filename"

#!/usr/bin/python
import os
import xml.etree.ElementTree as ET

#A helpful function to load compressed or uncompressed XML files
def loadXMLFile(filename):
    #Check if the file is compressed or not, and 
    if (os.path.splitext(filename)[1][1:].strip() == "bz2"):
        import bz2
        f = bz2.BZ2File(filename)
        doc = ET.parse(f)
        f.close()
        return doc
    else:
        return ET.parse(filename)

#Load the XML file config.out.xml
XMLDoc=loadXMLFile("config.out.xml")

#We can create a list of all particle tags using an xpath expression
#(xpath expressions always return lists)
PtTags = XMLDoc.findall("//Pt")

#Print the number of particles
print len(PtTags)

#print the x, y, and z positions of each particle
for PtElement in PtTags:
    PosTag = PtElement.find("P")
    print PosTag.get("x"), PosTag.get("y"), PosTag.get("z"), PtElement.get("D")

I don't know what is my mistake which I'm facing with this error? Is there any mistakes with directories? or perhaps some problems with file's name?

A V
  • 1

1 Answers1

0

You do not need to do your own edits. In the sample code, the filename is a parameter to the function, and should not be changed to a string. In the line where it says XMLDoc=loadXMLFile("config.out.xml"), the function is opening the file "config.out.xml" and using is as the filename parameter.

There's also problems with your else statement, but that's tied to not editing the function in the first place.

Try running the original code without your edits and see if any errors come up.

https://www.pythoncentral.io/fun-with-python-function-parameters/

Luke Ning
  • 147
  • 6
  • Thanks a lot for your help. The problem is when I leave the "filename" unchanged and run the code as you said without any modification, I faced with the same error. I took a screenshot to show : https://www.photobox.co.uk/my/photo/full?photo_id=501703268436 – A V Mar 04 '19 at 22:53
  • @AV That link doesn't work, I just see the Photobox home page. – Barmar Mar 04 '19 at 23:00