【发布时间】:2020-03-29 04:42:13
【问题描述】:
我正在转换一个 xml 文件,但 childs.attrib 的最后一行会覆盖某些列中其他行的值。
deCompressed = []
try:
for childs in root:
single = root.attrib
single.update(childs.attrib)
for child in childs:
single.update(child.attrib)
print(single)
deCompressed.append(single)
print(single) 显示我希望它们看起来的值。我认为问题在于update 语句的位置,因为print(childs.attrib) 给了我在所有行中打印的值。我尝试将语句移入和移出 for-loop 并使用 extend 而不是 append,但到目前为止没有运气。
我期待这样的输出:
{DocumentType: 111, DepartmentCode:AAA, SubCustomer: 'A11'}
{DocumentType: 123, DepartmentCode:BBB, SubCustomer: 'A12'}
{DocumentType: 145, DepartmentCode:CCC, SubCustomer: 'A13'}
相反,我得到了这个:
{DocumentType: 145, DepartmentCode: CCC, SubCustomer: 'A13'}
{DocumentType: 145, DepartmentCode: CCC, SubCustomer: 'A13'}
{DocumentType: 145, DepartmentCode: CCC, SubCustomer: 'A13'}
xml 输入:
<CompressedEntry SubCustomer="1687" LineNo="10000" ItemNo="18603" DepartmentCode="2105" CompressedEntryNo="33066">
<DetailedEntry DeliveryRoute="L40294" DistributionDate="2019-11-22" Distributor="" Initials="auto" Location="" PercentageRate="" ProjectCode="'00057126960639343453" Quantity="1.00" Rate="21.10" Amount="21.10" System="pak. fak. s. 01" SystemDate="2019-11-23" TransactionText="" DieselTaxRate="0.00" DieselTaxAmount="0.00" EntryNo="1136851" ItemName="Hjemmelevering 250-499 g" Weight="498.00" />
</CompressedEntry>
<CompressedEntry SubCustomer="1687" LineNo="630000" ItemNo="73310" DepartmentCode="2647" CompressedEntryNo="33128">
<DetailedEntry DeliveryRoute="321100" DistributionDate="2019-11-21" Distributor="" Initials="auto" Location="" PercentageRate="" ProjectCode="'00057126960623441509" Quantity="1.00" Rate="25.32" Amount="25.32" System="pak. fak. s. 02" SystemDate="2019-11-22" TransactionText="Pakkeshop retur" DieselTaxRate="0.00" DieselTaxAmount="0.00" EntryNo="1278046" ItemName="Pakkeshop retur 0-1.999 g" Weight="998.00" />
<DetailedEntry DeliveryRoute="122200" DistributionDate="2019-11-22" Distributor="" Initials="auto" Location="" PercentageRate="" ProjectCode="'00057126960638137305" Quantity="1.00" Rate="25.32" Amount="25.32" System="pak. fak. s. 02" SystemDate="2019-11-23" TransactionText="Pakkeshop retur" DieselTaxRate="0.00" DieselTaxAmount="0.00" EntryNo="1278047" ItemName="Pakkeshop retur 0-1.999 g" Weight="1605.00" />
</CompressedEntry>
第一个 CompressedEntry 被丢弃,最后一个值带有 f.e. DepartmentCode: 2647 打印在所有行中。
【问题讨论】:
-
我刚刚编辑了输出以更准确地显示它。我期望输出客户和子客户在所有行中都有不同的值。相反,最后一行的值会覆盖这两列中的值。
-
可以分享一下xml和完整代码吗?
-
尝试将有效的 XML 子集添加到帖子中。添加加载 XML 并解析它的代码部分。你想在这里做什么?收集 XML 路径中的所有属性?
-
查看 XML 内部我没有看到 Customer 属性。我确实看到了 SubCustomer
-
嘿秃头。首先:感谢您的浏览!我已经编辑了原始帖子,所以它显示了 XML 输入。我只是想转换 xml 文件的格式和命名。我继承了代码,原来的XML格式改变了,所以我尝试在Python中调整代码。