Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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 3.x 使用pythin解析pom.xml文件不';不能在较旧的python版本中工作_Python 3.x_Xml_Xpath_Elementtree - Fatal编程技术网

Python 3.x 使用pythin解析pom.xml文件不';不能在较旧的python版本中工作

Python 3.x 使用pythin解析pom.xml文件不';不能在较旧的python版本中工作,python-3.x,xml,xpath,elementtree,Python 3.x,Xml,Xpath,Elementtree,让我从一个明显的事实开始,我不是一个python开发人员——我主要用其他语言编写代码,因此如果这个问题有一个“明显的”答案,请原谅 我已经编写了一个非常简单的PomParser类,该类应该用作ElementTree的包装器,并将从pom.xml文件中以字符串形式返回一些值。下面的代码在Python 3.8中运行良好,但在Python>=3&&

让我从一个明显的事实开始,我不是一个python开发人员——我主要用其他语言编写代码,因此如果这个问题有一个“明显的”答案,请原谅

我已经编写了一个非常简单的
PomParser
类,该类应该用作
ElementTree
的包装器,并将从
pom.xml
文件中以字符串形式返回一些值。下面的代码在Python 3.8中运行良好,但在Python>=3&&<3.8中不起作用

将xml.etree.ElementTree作为ET导入
导入系统
从pathlib导入路径
庞巴瑟级:
data=”“”
org.example
scm:git:git://github.com
"""
树=无
名称空间={'':'http://maven.apache.org/POM/4.0.0'}
定义初始化(自):
self.tree=ET.fromstring(self.data)
def getTree(self):
回归自我树
def getGroupId(self):
返回self.findTextByXpath(“./groupId”)
def findTextByXpath(self,xpath:str):
element=self.findByXpath(xpath)
如果元素不是None,则返回element.text其他None
def findByXpath(self,xpath:str):
返回self.tree.find(xpath,self.namespace)
parser=PomParser()
打印(“Python:,sys.version)
打印(“不定义命名空间:”,parser.findTextByXpath(“./groupId”))
print(“显式定义命名空间:”,parser.getTree().findtext(“)/{http://maven.apache.org/POM/4.0.0}groupId“))
如果通过rept.it()运行此代码,它将使用Python 3.8打印:

Python:  3.8.3 (default, May 14 2020, 20:11:43) 
[GCC 7.5.0]
Without defining namespace:  org.example
Explicitly defining namespace:  org.example
Python:  3.4.3 (default, Nov 12 2018, 22:25:49)                                                                                                                                                                                                   
[GCC 4.8.4]                                                                                                                                                                                                                                       
Without defining namespace:  None                                                                                                                                                                                                                 
Explicitly defining namespace:  org.example   
但是,如果您在中运行相同的代码,它似乎在使用Python 3.4-它将打印:

Python:  3.8.3 (default, May 14 2020, 20:11:43) 
[GCC 7.5.0]
Without defining namespace:  org.example
Explicitly defining namespace:  org.example
Python:  3.4.3 (default, Nov 12 2018, 22:25:49)                                                                                                                                                                                                   
[GCC 4.8.4]                                                                                                                                                                                                                                       
Without defining namespace:  None                                                                                                                                                                                                                 
Explicitly defining namespace:  org.example   
我使用onlinegdb只是因为它再现了我在Netlify中遇到的问题,Netlify的最新版本是Python 3.7(实际上是我要添加支持的目标版本)

我想知道我在这里错过了什么? 我是否确实需要在xpath筛选器中显式定义名称空间?
如果是这样的话,那么当它什么都不做时,能够将
名称空间定义为参数的目的是什么呢???

这与Python 3.8中的更改有关。在该版本中,可以在名称空间映射中使用空字符串作为前缀。这在早期版本中不起作用

如果你改变

namespaces = {'': 'http://maven.apache.org/POM/4.0.0'}

改变

./groupId


它应该适用于所有版本的Python 3。

我认为这是相关的问题:您的解决方案比我所做的(使用
/{url}groupId
)更优雅!谢谢-看起来这为我解决了它!:)