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')
添加回答
舉報