好的,我有一个xml文件,其ID号码与其数量相匹配。 它们嵌套在xml中相当远,我假设它使问题更多。所以我需要为这些数字解析xml并最终以这种格式打印出来:126144/1 + 0000126151/1 + 0000167031/1 + 0000227496/3 +,6位数字是id和+之前的数字数量。 这是xml的一个示例:
<NAXML-BusDoc version="1.0">
<TransmissionHeader>
<TransmissionId/>
<TransmissionDate>20081031</TransmissionDate>
<TransmissionTime>173256</TransmissionTime>
<TransmissionStatus actionType="original"/>
</TransmissionHeader>
<Parties>
<Supplier>
<Name identType="CustVendID" ident="999999">Co</Name>
</Supplier>
<Buyer>
<Name identType="FEIN" ident="">PDIU M Mart Test</Name>
</Buyer>
</Parties>
<PurchaseOrder>
<PurchaseOrderNumber>12345</PurchaseOrderNumber>
<ShipTo identType="CustStoreID" ident="20 ">
<Name>M Mart #20</Name>
<Address/>
<City/>
<State/>
<PostalCode/>
</ShipTo>
<Dates>
<OrderDate>20160106</OrderDate>
</Dates>
<PurchaseOrderDetail>
<Terms/>
<LineItem count="1">
<PurchaseOrderUnit>
<PurchaseOrderUnitId identType="VIN" ident="**021360**"/>
<PurchaseOrderUnitQty UOMBasis="pack">**10**</PurchaseOrderUnitQty>
<BuyersCost UOMBasis="pack">12.1200</BuyersCost>
<Taxes taxable="No"/>
</PurchaseOrderUnit>
<RetailUnitPricing>
这是我到目前为止的代码:
from xml.dom import minidom
from itertools import imap
xmldoc = minidom.parse("newsample.xml")
itemlist = xmldoc.getElementsByTagName("PurchaseOrderUnitId")
quantity = xmldoc.getElementsByTagName("PurchaseOrderUnitQty")
for i in itemlist:
for s in quantity:
print "0000"+(i.attributes["ident"].value),"/"+s.firstChild.nodeValue+"+",
输出每个id号码6次,我不知道为什么。
0000021360/10 + 0000021360/2 + 0000021360/10 + 0000021360/5 + 0000021360/10 + 0000021360/15 + 0000021360/6 + 0000023408/10 + 0000023408/2 + 0000023408/10 +
答案 0 :(得分:0)
在缩进时,Python是一种非常挑剔的语言。我建议你阅读post on indentation。我认为你的具体问题来自这里:
final = ["0000",(i.attributes["ident"].value),"/","+",s.firstChild.nodeValue]
",".join(str(bit) for bit in final) # this line is tabbed, but that's not allowed!
print final # this line is tabbed twice, but it shouldn't be tabbed at all!
至于你的其他代码,我对xml并不是很熟悉,但看起来你最好通过阅读Python tutorial或Docs来获得最佳服务。