Во время использования приведенного ниже кода, который доступен в здесь , синтаксическая ошибка произойдет со мной, и я не знаю почему!Я догадался, что это потому, что я не установил упомянутую библиотеку в коде, но это не так.
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")
Вот оригинальный файл, в котором есть «имя файла»
#!/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")
Я не знаю, в чем моя ошибка, с которой я сталкиваюсь с этой ошибкой?Есть ли ошибки с каталогами?или, может быть, какие-то проблемы с именем файла?