[英]How to modify xml using Beautiful Soup?
我正在嘗試修改 xml 文件中的查找數據元素。 xml 的一個片段如下:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Options>
<SampleRate>1000</SampleRate>
<MaxStateSize>1</MaxStateSize>
<MaxOutputSize>1</MaxOutputSize>
</Options>
<CustomDefinitions>
<MyRser class="OhmicResistance">
<Object class="LookupObj2dWithState">
<RowState cacheref="Soc"/>
<ColState cacheref="ThermalState"/>
<LookupData>
0.02597518381655694900, 0.02513715386193249600, 0.02394715132636577100, 0.02325996676357371800, 0.02317075771456176400, 0.02277814077034603900, 0.02267913709322775700, 0.02258569292134297900, 0.02235026503875497600, 0.02222478423822949300, 0.02207606555239715500, 0.02198493491067361700, 0.02188144525929673300, 0.02167985791309091600, 0.02145797158835977700, 0.02137484908165417400, 0.02126561803424023600, 0.02124462299304301700, 0.02123310358079429400, 0.02126287857906075300, 0.02094998489960795500, 0.02073326148328196600, 0.02062489977511897100, 0.02038933084432985300;
</LookupData>
<MeasurementPointsRow desc="StateOfCharge">
-5, 0, 7.100000e+00, 1.120000e+01, 16, 2.080000e+01, 2.560000e+01, 3.040000e+01, 3.520000e+01, 4.010000e+01, 4.490000e+01, 4.970000e+01, 5.450000e+01, 5.930000e+01, 6.420000e+01, 69, 7.380000e+01, 7.860000e+01, 8.350000e+01, 8.830000e+01, 9.310000e+01, 9.770000e+01, 100, 105
</MeasurementPointsRow>
<MeasurementPointsColumn desc="ThermalState">
25
</MeasurementPointsColumn>
</Object>
</MyRser>
我想修改查找數據並使用該修改保存 xml 的副本。 這就是我的做法:
with open('....xml') as fp:
contents = fp.read()
soup = BeautifulSoup(contents, 'lxml')
tag = soup.find(elem_name).find(elem_path).lookupdata
tag.replace_with(str(values))
#saves the modified data as a new xml version
teslaname= elem_name+key
with open('modified.xml', 'w') as file:
file.write(str(soup))
file.close()
但是,當我這樣做時,特定的修改已經完成,但它改變了 xml 結構。
<?xml version="1.0" encoding="UTF-8"?><html><body><configuration>
<options>
<samplerate>1000</samplerate>
<maxstatesize>1</maxstatesize>
<maxoutputsize>1</maxoutputsize>
</options>
<customdefinitions>
<myrser class="OhmicResistance">
<object class="LookupObj2dWithState">
<rowstate cacheref="Soc"></rowstate>
<colstate cacheref="ThermalState"></colstate>
0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339, 0.02217779408499339
<measurementpointsrow desc="StateOfCharge">
-5, 0, 7.100000e+00, 1.120000e+01, 16, 2.080000e+01, 2.560000e+01, 3.040000e+01, 3.520000e+01, 4.010000e+01, 4.490000e+01, 4.970000e+01, 5.450000e+01, 5.930000e+01, 6.420000e+01, 69, 7.380000e+01, 7.860000e+01, 8.350000e+01, 8.830000e+01, 9.310000e+01, 9.770000e+01, 100, 105
</measurementpointsrow>
<measurementpointscolumn desc="ThermalState">
25
</measurementpointscolumn>
</object>
</myrser>
我想保留結構,只修改數據。 我知道它可以通過 ElementTree 完成,但是我需要我的代碼如何運行,beautifulsoup 使用起來更簡單。 那么如果只考慮使用beautifulsoup,如何在不丟失xml原始結構的情況下編輯並保存xml的副本? 任何幫助,將不勝感激。
要保持 XML 結構,請在寫入文件時使用.prettify()
方法:
file.write(str(soup.prettify()))
筆記:
BeautifulSoup
將 XML 標記轉換為小寫。
由於您使用上下文管理器打開文件,因此無需使用file.close()
關閉文件,文件將在退出縮進塊時自動關閉。
使用 lxml,您可以執行以下操作:
from lxml import etree
config = """[your xml above, corrected - it's not well formed]"""
new_values = "1,2,3,4"
doc = etree.XML(config.encode())
target = doc.xpath('//LookupData')[0]
target.text = new_values
print(etree.tostring(doc).decode())
輸出:
<Configuration>
<Options>
<SampleRate>1000</SampleRate>
<MaxStateSize>1</MaxStateSize>
<MaxOutputSize>1</MaxOutputSize>
</Options>
<CustomDefinitions>
<MyRser class="OhmicResistance">
<Object class="LookupObj2dWithState">
<RowState cacheref="Soc"/>
<ColState cacheref="ThermalState"/>
<LookupData>1,2,3,4</LookupData>
<MeasurementPointsRow desc="StateOfCharge">
-5, 0, 7.100000e+00, 1.120000e+01, 16, 2.080000e+01, 2.560000e+01, 3.040000e+01, 3.520000e+01, 4.010000e+01, 4.490000e+01, 4.970000e+01, 5.450000e+01, 5.930000e+01, 6.420000e+01, 69, 7.380000e+01, 7.860000e+01, 8.350000e+01, 8.830000e+01, 9.310000e+01, 9.770000e+01, 100, 105
</MeasurementPointsRow>
<MeasurementPointsColumn desc="ThermalState">
25
</MeasurementPointsColumn>
</Object>
</MyRser>
</CustomDefinitions>
</Configuration>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.