最后一行中的值将覆盖for循环中的其他行

2024-06-01 01:32:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在转换一个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的值被打印在所有行中


Tags: forupdateprintcccsingleattriba13childs
1条回答
网友
1楼 · 发布于 2024-06-01 01:32:10

见下文

import xml.etree.ElementTree as ET

xml = '''<SalesDocument DocumentType="Faktura" DocumentNo="80000538" PostingDate="2019-11-24" BillToCustNo="36721146-1020">
  <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="20000" ItemNo="18603" DepartmentCode="2115" CompressedEntryNo="33067">
    <DetailedEntry DeliveryRoute="B71907" DistributionDate="2019-11-22" Distributor="" Initials="auto" Location="" PercentageRate="" ProjectCode="'00057126960639323776" 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="1136852" ItemName="Hjemmelevering 250-499 g" Weight="498.00" />
  </CompressedEntry>
  <CompressedEntry SubCustomer="1687" LineNo="30000" ItemNo="18603" DepartmentCode="2140" CompressedEntryNo="33068">
    <DetailedEntry DeliveryRoute="B62349" DistributionDate="2019-11-20" Distributor="" Initials="auto" Location="" PercentageRate="" ProjectCode="'00057126960637514657" Quantity="1.00" Rate="21.10" Amount="21.10" System="pak. fak. s. 01" SystemDate="2019-11-21" TransactionText="" DieselTaxRate="0.00" DieselTaxAmount="0.00" EntryNo="1136853" ItemName="Hjemmelevering 250-499 g" Weight="498.00" />
  </CompressedEntry></SalesDocument>
'''
data = []
root = ET.fromstring(xml)
doc_type = root.attrib['DocumentType']
entries = root.findall('.//CompressedEntry')
for entry in entries:
    data.append({'DocumentType': doc_type, 'SubCustomer': entry.attrib['SubCustomer'],
                 'DepartmentCode': entry.attrib['DepartmentCode']})
print(data)

输出

[{'DocumentType': 'Faktura', 'SubCustomer': '1687', 'DepartmentCode': '2105'}, {'DocumentType': 'Faktura', 'SubCustomer': '1687', 'DepartmentCode': '2115'}, {'DocumentType': 'Faktura', 'SubCustomer': '1687', 'DepartmentCode': '2140'}]

相关问题 更多 >