Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在使用建议的python文件时遇到无效语法_Python_Syntax Error_Bz2 - Fatal编程技术网

在使用建议的python文件时遇到无效语法

在使用建议的python文件时遇到无效语法,python,syntax-error,bz2,Python,Syntax Error,Bz2,在使用中提供的以下代码时,我会出现语法错误,我不知道为什么!我猜这是因为我没有在代码中安装提到的库,但事实并非如此 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

在使用中提供的以下代码时,我会出现语法错误,我不知道为什么!我猜这是因为我没有在代码中安装提到的库,但事实并非如此

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")
这是包含“文件名”的原始文件


我不知道我在这个错误面前的错误是什么?目录有错误吗?或者文件名有问题?

您不需要自己编辑。在示例代码中,
文件名
是函数的参数,不应更改为字符串。在显示
XMLDoc=loadXMLFile(“config.out.xml”)
的行中,函数打开文件
“config.out.xml”
,并使用is作为
文件名
参数

else语句也有问题,但这与不首先编辑函数有关

尝试在不进行编辑的情况下运行原始代码,并查看是否出现任何错误


“语法错误”-哪个语法错误?如果您不发布它,我们无法猜测…您使用的是Python2还是Python3?您的
print
语句只能在Python2中使用。
loadXMLFile(“config.xml”)
语法无效,您的
else:
语句(由于缺少缩进)@Barmar我使用python3。。。Python3和Python2中的print语句有什么区别?在Python3中,
print
是一个函数,需要在参数周围加括号。非常感谢您的帮助。问题是,当我保持“文件名”不变,并按照您所说的那样运行代码而不做任何修改时,我会遇到同样的错误。我截图显示:@AV那个链接不起作用,我只看到Photobox主页。
#!/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")