Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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:xml.Find始终返回none_Python_Xml_Elementtree - Fatal编程技术网

Python:xml.Find始终返回none

Python:xml.Find始终返回none,python,xml,elementtree,Python,Xml,Elementtree,因此,我试图用python搜索并替换vcxproj文件中的xml关键字RunCodeAnalysis。 我对python还很陌生,所以请温柔一点,但我认为这将是做这类事情的最简单的语言。 我阅读了一些类似的示例,并给出了下面的代码,但无论我搜索ElementTree做什么,Find调用都不会返回任何结果 from xml.etree import ElementTree as et xml = '''\ <?xml version="1.0" encoding="utf-8"?>

因此,我试图用python搜索并替换vcxproj文件中的xml关键字RunCodeAnalysis。 我对python还很陌生,所以请温柔一点,但我认为这将是做这类事情的最简单的语言。 我阅读了一些类似的示例,并给出了下面的代码,但无论我搜索ElementTree做什么,Find调用都不会返回任何结果

from xml.etree import ElementTree as et

xml = '''\
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="12.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Protected_Debug|Win32'">
      <RunCodeAnalysis>false</RunCodeAnalysis>
   </PropertyGroup>
</Project>
'''

et.register_namespace('', "http://schemas.microsoft.com/developer/msbuild/2003")
tree = et.ElementTree(et.fromstring(xml))
print(tree.find('.//RunCodeAnalysis'))
从xml.etree导入ElementTree作为et
xml=“”\
假的
'''
et.register_命名空间(“”,“http://schemas.microsoft.com/developer/msbuild/2003")
tree=et.ElementTree(et.fromstring(xml))
打印(tree.find('.//RunCodeAnalysis'))
下面是一个简化的在线代码示例:


谁能告诉我我做错了什么吗?

好的。。所以@ThomWiggers帮我解决了丢失的部分——这是我的最后一段代码。还没有参数检查或任何类型的智能,但需要两个参数——文件名和是否将静态代码分析转换为true或false。我有大约30个项目,我想在夜间构建时打开它,但我真的不想每天打开它,因为它太慢了

import sys
from xml.etree import ElementTree as et

et.register_namespace('', "http://schemas.microsoft.com/developer/msbuild/2003")
tree = et.parse(sys.argv[1])
value = sys.argv[2]
for item in tree.findall('.//{http://schemas.microsoft.com/developer/msbuild/2003}RunCodeAnalysis'):
    item.text = value
for item in tree.findall('.//{http://schemas.microsoft.com/developer/msbuild/2003}EnablePREfast'):
    item.text = value
tree.write(sys.argv[1])

我不知道为什么会这样,但是
>>tree.find('{http://schemas.microsoft.com/developer/msbuild/2003}PropertyGroup')
真管用啊!谢谢@ThomWiggers-所以我可以使用:for tree.findall('./{}runcodealysis'):item.text='true',这就是我想要的。现在我需要做的就是弄清楚为什么我使用tree.write()时它会删除这个部分,这样我就可以让这个脚本正常工作了