繁体   English   中英

使用Python从xml文件中提取数据并写入xlsxwriter

[英]Extracting data from xml file using Python and writing to xlsxwriter

我正在尝试使用python提取XML文件的输出,并使用xlsxwriter模块将其写入excel工作表。

下面给出的是我尝试过的代码:

row = 4
col = 0
row1 = 5
col1 = 0

for elem in tree.iter():
    worksheet1.write(row, col, elem.tag)
    for subelem in elem:
        worksheet1.write(row1, col1, subelem.text)
    col += 1
    col1 +=1

上面的返回值标头,但不返回任何相应的值。

我正在尝试存储它,以使第一行具有所有标签的列表,而第二行具有xml文件中提供的相应数据。

我正在尝试的数据是:

<?xml version="1.0" encoding="UTF-8"?><PARENT>
  <CHILD>
<Action>add</Action>
<BillNo>6446</BillNo>
<CustomerID/>
<Customer>
  <Name/>
  <CustCode>ABC</CustCode>
</Customer>
<Remarks>
  <Remark>
HELLO</Remark>
  <Remarks>123</Remarks>
</Remarks>
<Store>sf</Store>
<StoreType/>
<Urgency>false</Urgency>
<StoreTypes>
  <StoreType>
    <Action>new</Action>
    <Name>Type1</Name>
    <StoreID>46433</StoreID>
    <StopAlias>1</StopAlias>
    <Type>45643</Type>
    <Type1>dsff</Type1>
    <Type2>egrg</Type2>
    <Type3>geetf</Type3>
    <Type4/>
    <Type5>khfd</Type5>
    <Type6>sfgdg</Type6>
    <Type7>dsfee</Type7>
  </StoreType>
</StoreTypes>
<Category1>
  <CatGroup>
    <Action>new</Action>
    <D1>hello</D1>
    <D2>world</D2>
    <D3>2</D3>
    <Type>how</Type>
    <D4>dfvf</D4>
    <D5>david</D5>
    <D6>f5453</D6>
    <D7>this</D7>
    <D8>is</D8>
    <D9>a</D9>
    <Type4/>
    <Feedback/>
    <Customer>
      <F1>test</F1>
      <F2>remark</F2>
      <F3>file</F3>
    </Customer>
    <R1>
      <RR1>for</RR1>
      <RR1>test</RR1>
      <RR1>tested</RR1>
    </R1>
  </CatGroup>
 </Category1>
 </CHILD>
 </PARENT>

任何人都可以建议我哪里错了。 我试图保持这种通用而不循环时定义列名称。 谢谢

从python文档中查看此stackabuse文章或xml.etree.ElementTree链接。

两者都包含有关如何阅读xml文档的分步示例。

这是第一个链接中的一些示例代码:

from xml.dom import minidom

# parse an xml file by name
mydoc = minidom.parse('items.xml')

items = mydoc.getElementsByTagName('item')

# one specific item attribute
print('Item #2 attribute:')  
print(items[1].attributes['name'].value)

# all item attributes
print('\nAll attributes:')  
for elem in items:  
    print(elem.attributes['name'].value)

# one specific item's data
print('\nItem #2 data:')  
print(items[1].firstChild.data)  
print(items[1].childNodes[0].data)

# all items data
print('\nAll item data:')  
for elem in items:  
    print(elem.firstChild.data)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM