2016-09-07 23 views
1

私はpythonに新しいです。実際に私はXMLを解析することができますが、これをExcelに書き込む方法はわかりません。python - excelに書き込む

サンプルXML:

<?xml version="1.0" encoding="UTF-8"?> 
<breakfast_menu> 
    <food> 
     <name>Belgian Waffles</name> 
     <price>$5.95</price> 
     <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description> 
     <calories>650</calories> 
    </food> 
    <food> 
     <name>Strawberry Belgian Waffles</name> 
     <price>$7.95</price> 
     <description>Light Belgian waffles covered with strawberries and whipped cream</description> 
     <calories>900</calories> 
    </food> 

私のコード:

import xml.etree.ElementTree as etree 
xmld = etree.parse('simple.xml') 
root = xmld.getroot() 
for child in root: 
    for children in child: 
    print children.tag," : % s" %children.text 

出力:

name : Belgian Waffles 
price : $5.95 
description : Two of our famous Belgian Waffles with plenty of real maple syrup 
calories : 650 
name : Strawberry Belgian Waffles 
price : $7.95 
description : Light Belgian waffles covered with strawberries and whipped cream 
calories : 900 

列Aに

'タグ' などのExcelにこれを記述する必要があります列に「値」を入力しますB enter image description here

xlwtで試してみました

またはopenpyxlや運をpandas..butない... plsはこのサイトから...

+0

あなたの出力から辞書を作ってから、それからデータフレームを作り、それをExcelファイルに書き込んでみてください。 –

+0

この問題は実際のものですか、それとも答えがありますか? –

+0

こんにちはVlad ... yepはopenpyxlのように試しました: "zip({children.tag}、{children.text})の行: ws.append(行) wb.save(" simple.xls " ) " 私は期待どおりの出力を得ることができます。どうもありがとう!!!! –

答えて

1

感謝ヴラド.. 私は以下のとおりに私のコードを改善している:

import xml.etree.ElementTree as etree 
from openpyxl import Workbook 
xmld = etree.parse('simple.xml') 
root = xmld.getroot() 
wb = Workbook() 
ws = wb.active 
for child in root: 
    for children in child: 
    #print children.tag," : % s" %children.text 
    for row in zip({children.tag},{children.text}): 
     ws.append(row) 
     wb.save("simple.xls") 

今私が期待通りにExcelの列に書き込むに成功メートル。

+0

私はそれがうまくいってうれしい! –

関連する問題