1 回答
TA贡献1828条经验 获得超6个赞
你可以学习xml并且因为主要问题与但是xpath无关。pandasxml
您可以使用更复杂的方法xpath来查找具有名称M_20_K40745170和子节点的节点mwam,您必须在其中搜索pre和更新它(甚至添加新的pre)
node = root.find('./design/sec[@name="M_20_K40745170"]//mwan')
你可以用df.iterrows()这个
for index, row in df.iterrows():
node = root.find('./design/sec[@name="{}"]//mwan'.format(row['name']))
稍后您可以per使用以下命令进行搜索"Volum_5mb"
item = node.find('./per[@fre="Volum_5mb"]')
并创建新的和/或更新值
if not item: # if item is None:
item = ET.SubElement(node, 'per')
item.set('fre', "Volum_5mb")
item.set('value', str(row['Volum_5mb']))
你可以使用列表['Volum_5mb', 'Volum_40mb', 'Volum_70mb']来做到这一点
for fre in ['Volum_5mb', 'Volum_40mb', 'Volum_70mb']:
item = node.find('./per[@fre="{}"]'.format(fre))
#print(fre, item)
if not item:
item = ET.SubElement(node, 'per')
item.set('fre', fre)
item.set('value', str(row[fre]))
直接在代码中包含示例数据的最小工作代码,但您应该从文件中读取它们。
text = ''' name Volum_5mb Volum_40mb Volum_70mb
1 M_20_K40745170 89.00 44.00 77.00
2 M_20_K40745171 77.00 65.00 94.00'''
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<brand by="hhdhdh" date="2014/01/01" name="OOP-112200" Insti="TGA">
<design name="OOP-112200" own="TGA" descri="" sound_db="JJKO">
<sec name="abcd" sound_freq="abcd" c_ty="pv">
<feature number="48">
<tfgt v="0.1466469683747654" y="0.0" units="sec" />
</feature>
<mwan sound_freq="abcd" first_name="g7tty" description="xyz" />
</sec>
<sec name="M_20_K40745170" sound_freq="mhr17:7907527-7907589" tension="SGCGSCGSCGSCGSC" s_c="0">
<feature number="5748">
<tfgt v="0.1466469683747654" y="0.0" units="sec" />
</feature>
<mwan sound_freq="mhr17:7907527-7907589" first_name="g7tty" description="xyz">
</mwan>
</sec>
<sec name="M_20_K40745171" sound_freq="mhr17:7907528-7907599" tension="SGCGSCGSCGSHHGSC" s_c="0">
<feature number="5748">
<tfgt v="0.1466469683747654" y="0.0" units="sec" />
</feature>
<mwan sound_freq="mhr17:7907527-7907589" first_name="gtftty" description="xyz">
<xyz abc="trt" id="abc" />
<per fre="acc" value="abc" />
<per fre="xyz" value="abc" />
<per fre="yy" value="abc" />
</mwan>
</sec>
</design>
</brand>'''
import pandas as pd
import io
import xml.etree.ElementTree as ET
#df = pd.read_csv('input.csv')
df = pd.read_csv(io.StringIO(text), sep='\s+')
#print(df)
#tree = ET.('input.xml')
#root = tree.getroot()
root = ET.fromstring(xml)
tree = ET.ElementTree(root)
for index, row in df.iterrows():
node = root.find('./design/sec[@name="{}"]//mwan'.format(row['name']))
for fre in ['Volum_5mb', 'Volum_40mb', 'Volum_70mb']:
item = node.find('./per[@fre="{}"]'.format(fre))
#print('item:', fre, '=', item)
if not item:
#print('new', item, fre)
item = ET.SubElement(node, 'per')
#item.tail = '\n ' # to pretty print
item.set('fre', fre)
item.set('value', str(row[fre]))
#print(ET.tostring(node).decode())
#---
print( ET.tostring(root) )
#tree.write('output.xml')
添加回答
举报