首页 > 解决方案 > 将数据框导出到 Excel 表

问题描述

下面的简单代码在数据框中打印某些元素及其属性。它遍历 XML 文件,查找这些元素并将它们打印出来

代码

import xml.etree.ElementTree as ET
import pandas as pd
tree = ET.parse('1last.xml')
root = tree.getroot()

for neighbor in root.iter('Description'):
    print(neighbor.attrib, neighbor.text)
for neighbor in root.iter('SetData'):
    print(neighbor.attrib)
for neighbor in root.iter('FileX'):
    print(neighbor.attrib) 
for neighbor in root.iter('FileY'):
    print(neighbor.attrib)

输出

这里

我想将输出导出到 Excel 表格中,但它似乎不起作用我已经尝试过了

export_excel = root.to_excel (r'C:\Users\fsdf.LAPTOP-E8A1PPIN\Desktop\test\export_dataframe.xlsx', index = None, header=True)

但我收到错误消息“AttributeError:‘xml.etree.ElementTree.Element’对象没有属性‘to_excel’

这是我的 xml 文件

<?xml version="1.0" encoding="utf-8"?>
<ProjectData>
<FINAL>
    <START id="ID0001" service_code="0x5196">
      <Docs Docs_type="START">
        <Rational>225196</Rational>
        <Qualify>6251960000A0DE</Qualify>
      </Docs>
      <Description num="1213f2312">The parameter</Description>
      <SetFile dg="" dg_id="">
        <SetData value="32" />
      </SetFile>
    </START>
    <START id="DG0003" service_code="0x517B">
      <Docs Docs_type="START">
        <Rational>23423</Rational>
        <Qualify>342342</Qualify>
      </Docs>
      <Description num="3423423f3423">The third</Description>
      <SetFile dg="" dg_id="">
        <FileX dg="" axis_pts="2" name="" num="" dg_id="" />
        <FileY unit="" axis_pts="20" name="TOOLS" text_id="23423" unit_id="" />
        <SetData x="E1" value="21259" />
        <SetData x="E2" value="0" />
      </SetFile>
    </START>
    <START id="ID0048" service_code="0x5198">
      <RawData rawdata_type="OPDATA">
        <Request>225198</Request>
        <Response>343243324234234</Response>
      </RawData>
      <Meaning text_id="434234234">The forth</Meaning>
      <ValueDataset unit="m" unit_id="FEDS">
        <FileX dg="kg" discrete="false" axis_pts="19" name="weight" text_id="SDF3" unit_id="SDGFDS" />
        <SetData xin="sdf" xax="233" value="323" />
        <SetData xin="123" xax="213" value="232" />
        <SetData xin="2321" xax="232" value="23" />
      </ValueDataset>
    </START>
</FINAL>
</ProjectData>

这就是我希望桌子看起来的样子。 在此处输入图像描述

标签: pythondataframeexport-to-csvopenpyxlxlsx

解决方案


一种方法是使用库,例如openpyxl​​直接编写 Excel 文件。下面显示了如何做到这一点:

import openpyxl    
from bs4 import BeautifulSoup


with open('1last.xml') as f_input:
    soup = BeautifulSoup(f_input, 'lxml')

wb = openpyxl.Workbook()
ws = wb.active
ws.title = "Sheet1"

ws.append(["Description", "num", "text"])

for description in soup.find_all("description"):
    ws.append(["", description['num'], description.text])

ws.append(["SetData", "x", "value", "xin", "xax"])

for setdata in soup.find_all("setdata"):
    ws.append(["", setdata.get('x', ''), setdata.get('value', ''), setdata.get('xin', ''), setdata.get('xax', '')])

wb.save(filename="1last.xlsx")

这将创建一个 Excel 文件,如下所示:

Excel 截图


推荐阅读