Python XML编辑属性
我想编辑XML文件中元素的属性 文件看起来像Python XML编辑属性,python,xml,Python,Xml,我想编辑XML文件中元素的属性 文件看起来像 <Parameter name="Spec 2 Circumference/Length" type="real" mode="both"> <Value>0.0</Value> <Result>0.0</Result> </Parameter> 0 0 我想用文本文件中的其他值替换value和Result属性 请建议。 提前感谢。使用的示例。它将用一些
<Parameter name="Spec 2 Circumference/Length" type="real" mode="both">
<Value>0.0</Value>
<Result>0.0</Result>
</Parameter>
0
0
我想用文本文件中的其他值替换value和Result属性
请建议。
提前感谢。使用的示例。它将用一些字符串替换值
元素文本;结果
元素的程序是模拟的,此处省略:
#!/usr/bin/env python
xml = """
<Parameter name="Spec 2 Circumference/Length" type="real" mode="both">
<Value>0.0</Value>
<Result>0.0</Result>
</Parameter>
"""
from elementtree.ElementTree import fromstring, tostring
# read XML, here we read it from a String
doc = fromstring(xml)
for e in doc.findall('Value'):
e.text = 'insert your string from your textfile here!'
print tostring(doc)
# will result in:
#
# <Parameter mode="both" name="Spec 2 Circumference/Length" type="real">
# <Value>insert your string from your textfile here!</Value>
# <Result>0.0</Result>
# </Parameter>
#/usr/bin/env python
xml=”“”
0
0
"""
从elementtree.elementtree导入从字符串到字符串
#读取XML,这里我们从字符串读取它
doc=fromstring(xml)
对于文件findall中的e(“值”):
e、 text='在此处插入文本文件中的字符串!'
打印到字符串(文档)
#将导致:
#
#
#在此处插入文本文件中的字符串!
# 0.0
#
使用的示例。它将用一些字符串替换值
元素文本;结果
元素的程序是模拟的,此处省略:
#!/usr/bin/env python
xml = """
<Parameter name="Spec 2 Circumference/Length" type="real" mode="both">
<Value>0.0</Value>
<Result>0.0</Result>
</Parameter>
"""
from elementtree.ElementTree import fromstring, tostring
# read XML, here we read it from a String
doc = fromstring(xml)
for e in doc.findall('Value'):
e.text = 'insert your string from your textfile here!'
print tostring(doc)
# will result in:
#
# <Parameter mode="both" name="Spec 2 Circumference/Length" type="real">
# <Value>insert your string from your textfile here!</Value>
# <Result>0.0</Result>
# </Parameter>
#/usr/bin/env python
xml=”“”
0
0
"""
从elementtree.elementtree导入从字符串到字符串
#读取XML,这里我们从字符串读取它
doc=fromstring(xml)
对于文件findall中的e(“值”):
e、 text='在此处插入文本文件中的字符串!'
打印到字符串(文档)
#将导致:
#
#
#在此处插入文本文件中的字符串!
# 0.0
#
0.0 0.0是有效的XML吗?请发布更多文件..0.0 0.0是有效的XML吗?请多发一些你的文件,谢谢你的建议。实际上,在实际情况中,我有一个重复包含这些元素的XML文件。我想读取XMl文件中的行,然后按照您所说的做。请给我一些提示。感谢您的即时回复。请修改您的问题(以上),而不是在评论中提出新问题。。这样更容易理解;此外,请始终在问题中包含代码/数据的相关部分,可能还有一些提示,说明您遇到了什么问题以及原因(错误、异常等)-最后一点:如果不是“社区维基”,更多人(代表猎手)会回答这个问题,非常感谢您的建议。实际上,在实际情况中,我有一个重复包含这些元素的XML文件。我想读取XMl文件中的行,然后按照您所说的做。请给我一些提示。感谢您的即时回复。请修改您的问题(以上),而不是在评论中提出新问题。。这样更容易理解;此外,请始终在问题中包含代码/数据的相关部分,并可能提供一些提示,说明您遇到了什么问题以及原因(错误、异常等)-最后一点:如果不是“社区维基”,更多人(代表猎手)将回答此问题