【发布时间】:2016-09-22 20:56:49
【问题描述】:
我无法遍历下面的 Python 列表并将 xml 属性替换为所有 ADSType 值。
Python 字典
{'ADSType': ['HS11N', 'HS11V'], 'Type': ['Bond', 'Cash']}
XML
我想将 XML 中每一行的 sid 值替换为 ADS 值。
<req Times="1" action="get" chunklimit="1000" lang="ENU" msg="1" rank="1" rnklst="1" runuf="0">
<flds>
<f end="2016-02-29" freq="m" i="bond(long) hff" sid="abc" start="2016-02-29" />
<f end="2016-02-29" freq="m" i="bond(short) ggg" sid="abc" start="2016-02-29" />
</flds>
<dat>
<r CalculationType="3" ForceCalculate="1" i="123" />
</dat>
</req>
到目前为止的 Python 代码:
data_list = {'ADSType': ['HS11N', 'HS11V'], 'Type': ['Bond', 'Cash']}
xml_file = './test.xml'
tree1 = ET.ElementTree(file=xml_file)
root1 = tree1.getroot()
for x in root1.iter('flds'):
for y in x.iter('f'):
y.set('sid', y.get('sid').replace("123", "abc"))
# print(ET.tostring(root1).decode("UTF-8"))
print "test"
# print(ET.tostring(root1).decode("UTF-8"))
tree1.write("./test.xml")
我被困在如何迭代列表、提取值、动态输入替换方法和更新 xml。请帮忙!以下是我想要实现的期望输出。
<req Times="1" action="get" chunklimit="1000" lang="ENU" msg="1" rank="1" rnklst="1" runuf="0">
<flds>
<f end="2016-02-29" freq="m" i="bond(long) hff" sid="HS11N" start="2016-02-29" />
<f end="2016-02-29" freq="m" i="bond(short) ggg" sid="HS11V" start="2016-02-29" />
</flds>
<dat>
<r CalculationType="3" ForceCalculate="1" i="123" />
</dat>
</req>
【问题讨论】:
-
为什么会有债券和现金? dict 是如何融入其中的?
标签: python xml loops dictionary