Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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 我在ElementTree上遇到一个奇怪的保存错误_Python_Key_Elementtree - Fatal编程技术网

Python 我在ElementTree上遇到一个奇怪的保存错误

Python 我在ElementTree上遇到一个奇怪的保存错误,python,key,elementtree,Python,Key,Elementtree,我当前的程序必须更改xml值,为此,我使用ElementTree进行以下配置: for file in sorted: import xml.etree.ElementTree as ET tree = ET.parse(file) root = tree.getroot() value = root.find(".//*[@key='GainedXP']").text root.find(value).text = 9999999 ET.tos

我当前的程序必须更改xml值,为此,我使用ElementTree进行以下配置:

for file in sorted:
    import xml.etree.ElementTree as ET
    tree = ET.parse(file)
    root = tree.getroot()
    value = root.find(".//*[@key='GainedXP']").text

    root.find(value).text = 9999999
    ET.tostring(root)

但是代码没有改变任何值,python崩溃,掌握它的技巧……

好的,我自己解决了这个问题。我又看了一遍新纪录片,注意到我必须写:

tree.write(f)
插入:

ET.tostring(root)

干杯

这有点有趣,因为我的标签是一个很好的押韵xD…得到一个奇怪的保存错误。。。发布有关产生异常的代码的问题时,请始终包含完整的回溯-复制并粘贴它,然后将其格式化为代码。从文档中选择它并键入ctrl-k以查找标记,然后为该标记的文本属性指定新值。这就是你在做的吗?也许您可以根据您的情况调整该示例。错误是:AttributeError:'NoneType'对象没有属性'text',然后这两个查找操作中的一个没有返回任何属性。回溯会告诉你哪一个-这就是为什么在你的问题中包含完整的回溯很重要。如果您的代码依赖于处理数据,那么您应该包含一个最小的数据示例—刚好足够的代码和数据来重现问题,即使您需要为我们构建一个玩具示例。有时,为问题构建mre将为您突出问题。您是否尝试在文档中改编该示例?