我正在尝试使用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>
谁能建议我要去哪里错了。我试图保持这种通用而不循环时定义列名称。谢谢
答案 0 :(得分:0)
从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)