将数据框导出到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表

我想将输出导出到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>

这就是我希望表格看起来像的样子。

将数据框导出到Excel表

oji6695 回答:将数据框导出到Excel表

一种方法是使用诸如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 screenshot

本文链接:https://www.f2er.com/3044993.html

大家都在问