我正在转换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。 DepartmentCode:2647在所有行中打印。
答案 0 :(得分:0)
见下文
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'}]