我正在转换一个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的值被打印在所有行中
见下文
输出
相关问题 更多 >
编程相关推荐