xml.etree.ElementTree问题之xmlns:ns0="http://www.fnfr.com/schemas/parameterTree"

在使用Python的xml.etree.ElementTree模块解析XML文件时,遇到了头部信息自动合并导致的错误。文章详细描述了错误现象,并分享了从xml.etree.ElementTree切换到xml.dom的方法来解决这一问题的经验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上周在运行Python脚本时,发现解析后的XML文件出现了如下BUG:
1、打开XML文件,再运行脚本,一切正常,XML文件及时更新,需要修改的目标文字也修改成功。
2、运行脚本,再打开XML文件,发现XML文件的头部自动合并了,出现了xmlns:ns0=“http://www.fnfr.com/schemas/parameterTree” 这么一段文字。在后期的调用XML文件时,这句话也被报错了。

原文件头部

初始文件头部

变成下面这种情况

合并后的情况

经查找,网上对这方面错误的描述了了,后询问部门的大佬。大佬表示xml.etree.ElementTre方法确实存在这个问题,建议更换成xml.dom方法。

现分享一下我遇到的这种请求,大佬的建议也供大家参考一下。
如果有小伙伴知道原因或者有其他解释方法,望分享我一下下,爱你。

<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <SOAP-ENV:Body> <SOAP-ENC:Array xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Tsr2/Tsr2%2C%20Version%3D2.1.3.9%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull" SOAP-ENC:arrayType="a1:ModPCBY+structComSetting[10]"> <item> <Baurate>9600</Baurate> <Parity id="ref-3">None</Parity> <Databit>8</Databit> <Stopbit>1</Stopbit> <Term>13</Term> <Timeout>1000</Timeout> <Wait>1200</Wait> <Head>0</Head> </item> <item> <Baurate>9600</Baurate> <Parity id="ref-4">Even</Parity> <Databit>7</Databit> <Stopbit>2</Stopbit> <Term>3</Term> <Timeout>1000</Timeout> <Wait>0</Wait> <Head>2</Head> </item> <item> <Baurate>9600</Baurate> <Parity id="ref-5">None</Parity> <Databit>8</Databit> <Stopbit>1</Stopbit> <Term>13</Term> <Timeout>1000</Timeout> <Wait>0</Wait> <Head>48</Head> </item> <item> <Baurate>9600</Baurate> <Parity id="ref-6">None</Parity> <Databit>8</Databit> <Stopbit>1</Stopbit> <Term>13</Term> <Timeout>2000</Timeout> <Wait>0</Wait> <Head>0</Head> </item> <item> <Baurate>38400</Baurate> <Parity id="ref-7">None</Parity> <Databit>8</Databit> <Stopbit>1</Stopbit> <Term>13</Term> <Timeout>3000</Timeout> <Wait>0</Wait> <Head>0</Head> </item> <item> <Baurate>9600</Baurate> <Parity id="ref-8">None</Parity> <Databit>8</Databit> <Stopbit>1</Stopbit> <Term>13</Term> <Timeout>1000</Timeout> <Wait>0</Wait> <Head>0</Head> </item> <item> <Baurate>9600</Baurate> <Parity id="ref-9">None</Parity> <Databit>8</Databit> <Stopbit>1</Stopbit> <Term>13</Term> <Timeout>1000</Timeout> <Wait>0</Wait> <Head>0</Head> </item> <item> <Baurate>9600</Baurate> <Parity id="ref-10">None</Parity> <Databit>8</Databit> <Stopbit>1</Stopbit> <Term>13</Term> <Timeout>1000</Timeout> <Wait>0</Wait> <Head>0</Head> </item> <item> <Baurate>9600</Baurate> <Parity id="ref-11">None</Parity> <Databit>8</Databit> <Stopbit>1</Stopbit> <Term>13</Term> <Timeout>1000</Timeout> <Wait>0</Wait> <Head>0</Head> </item> <item> <Baurate>0</Baurate> <Parity xsi:null="1"/> <Databit>0</Databit> <Stopbit>0</Stopbit> <Term>0</Term> <Timeout>0</Timeout> <Wait>0</Wait> <Head>0</Head> </item> </SOAP-ENC:Array> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
07-23
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac x16r2 xr xr9" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:x16r2="http://schemas.microsoft.com/office/spreadsheetml/2015/02/main" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr9="http://schemas.microsoft.com/office/spreadsheetml/2016/revision9"> <numFmts count="1"> <numFmt numFmtId="176" formatCode=""$"#,##0.00_);[Red]\("$"#,##0.00\)"/> </numFmts> <fonts count="8" x14ac:knownFonts="1"> <font> <sz val="11"/> <color theme="1"/> <name val="Garamond"/> <family val="2"/> <scheme val="minor"/> </font> <font> <b/> <sz val="15"/> <color theme="3"/> <name val="Aharoni"/> <family val="2"/> <scheme val="major"/> </font> <font> <b/> <sz val="13"/> <color theme="3"/> <name val="Aharoni"/> <family val="2"/> <scheme val="major"/> </font> <font> <sz val="11"/> <color theme="1"/> <name val="Garamond"/> <family val="2"/> <scheme val="minor"/> </font> <font> <sz val="11"/> <color theme="0"/> <name val="Garamond"/> <family val="2"/> <scheme val="minor"/> </font> <font> <sz val="9"/> <name val="宋体"/> <family val="3"/> <charset val="134"/> <scheme val="minor"/> </font> <font> <b/> <sz val="72"/> <color theme="3" tint="-0.499984740745262"/> <name val="Aharoni"/> <scheme val="major"/> </font> <font> <b/> <sz val="28"/> <color theme="3" tint="-0.499984740745262"/> <name val="Aharoni"/> <scheme val="major"/> </font> </fonts> <fills count="3"> <fill> <patternFill patternType="none"/> </fill> <fill> <patternFill patternType="gray125"/> </fill> <fill> <patternFill patternType="solid"> <fgColor theme="3"/> <bgColor indexed="64"/> </patternFill> </fill> </fills> <borders count="5"> <border> <left/> <right/> <top/> <bottom/> <diagonal/> </border> <border> <left/> <right/> <top/> <bottom style="thick"> <color theme="4"/> </bottom> <diagonal/> </border> <border> <left/> <right/> <top/> <bottom style="thick"> <color theme="4" tint="0.499984740745262"/> </bottom> <diagonal/> </border> <border> <left style="thin"> <color indexed="64"/> </left> <right style="thin"> <color indexed="64"/> </right> <top style="thin"> <color indexed="64"/> </top> <bottom style="thin"> <color indexed="64"/> </bottom> <diagonal/> </border> <border> <left style="thin"> <color indexed="64"/> </left> <right/> <top style="thin"> <color indexed="64"/> </top> <bottom style="thin"> <color indexed="64"/> </bottom> <diagonal/> </border> </borders> <cellStyleXfs count="7"> <xf numFmtId="0" fontId="0" fillId="0" borderId="0"> <alignment wrapText="1"/> </xf> <xf numFmtId="0" fontId="1" fillId="0" borderId="1"> <alignment horizontal="left"/> </xf> <xf numFmtId="0" fontId="2" fillId="0" borderId="2" applyNumberFormat="0" applyFill="0" applyAlignment="0" applyProtection="0"/> <xf numFmtId="176" fontId="3" fillId="0" borderId="0" applyFont="0" applyFill="0" applyBorder="0" applyProtection="0"> <alignment horizontal="right"/> </xf> <xf numFmtId="10" fontId="3" fillId="0" borderId="0" applyFont="0" applyFill="0" applyBorder="0" applyProtection="0"> <alignment horizontal="right"/> </xf> <xf numFmtId="0" fontId="1" fillId="0" borderId="1"> <alignment horizontal="left" vertical="center"/> </xf> <xf numFmtId="0" fontId="4" fillId="0" borderId="0" applyNumberFormat="0" applyFill="0" applyBorder="0" applyAlignment="0"> <alignment horizontal="center" wrapText="1"/> </xf> </cellStyleXfs> <cellXfs count="4"> <xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"> <alignment wrapText="1"/> </xf> <xf numFmtId="0" fontId="0" fillId="0" borderId="3" xfId="0" applyBorder="1"> <alignment wrapText="1"/> </xf> <xf numFmtId="0" fontId="6" fillId="2" borderId="3" xfId="5" applyFont="1" applyFill="1" applyBorder="1" applyAlignment="1"> <alignment horizontal="left" wrapText="1" indent="1"/> </xf> <xf numFmtId="0" fontId="7" fillId="2" borderId="4" xfId="0" applyFont="1" applyFill="1" applyBorder="1" applyAlignment="1"> <alignment horizontal="left" vertical="top" wrapText="1" indent="2"/> </xf> </cellXfs> <cellStyles count="7"> <cellStyle name="zHiddenText" xfId="6" xr:uid="{F6161882-ABBC-4CDD-9369-B0FFFF270934}"/> <cellStyle name="百分比" xfId="4" builtinId="5" customBuiltin="1"/> <cellStyle name="标题" xfId="5" builtinId="15" customBuiltin="1"/> <cellStyle name="标题 1" xfId="1" builtinId="16" customBuiltin="1"/> <cellStyle name="标题 2" xfId="2" builtinId="17" customBuiltin="1"/> <cellStyle name="常规" xfId="0" builtinId="0" customBuiltin="1"/> <cellStyle name="货币" xfId="3" builtinId="4" customBuiltin="1"/> </cellStyles> <dxfs count="3"> <dxf> <font> <b/> <i val="0"/> </font> <fill> <patternFill> <bgColor theme="4" tint="0.79998168889431442"/> </patternFill> </fill> </dxf> <dxf> <fill> <patternFill> <bgColor theme="4" tint="0.59996337778862885"/> </patternFill> </fill> </dxf> <dxf> <fill> <patternFill> <bgColor theme="4" tint="0.79998168889431442"/> </patternFill> </fill> </dxf> </dxfs> <tableStyles count="1" defaultTableStyle="TableStyleLight9" defaultPivotStyle="PivotStyleLight16"> <tableStyle name="Table Style 1" pivot="0" count="3" xr9:uid="{45B6FF1C-BF4F-AB45-B97A-E3509B3D88C4}"> <tableStyleElement type="wholeTable" dxfId="2"/> <tableStyleElement type="headerRow" dxfId="1"/> <tableStyleElement type="totalRow" dxfId="0"/> </tableStyle> </tableStyles> <extLst> <ext uri="{EB79DEF2-80B8-43e5-95BD-54CBDDF9020C}" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main"> <x14:slicerStyles defaultSlicerStyle="SlicerStyleLight1"/> </ext> <ext uri="{9260A510-F301-46a8-8635-F512D64BE5F5}" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main"> <x15:timelineStyles defaultTimelineStyle="TimeSlicerStyleLight1"/> </ext> </extLst> </styleSheet> 这里面的标签都是什么意思
06-28
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值