【问题标题】:Values from last row overwrites other rows inside for loop最后一行的值会覆盖 for 循环中的其他行
【发布时间】: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中调整代码。

标签: python xml for-loop


【解决方案1】:

见下文

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'}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多