python - 将数据框导出到 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>
解决方案
一种方法是使用库,例如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 文件,如下所示:
推荐阅读
- mysql - DATEDIFF 函数在 sqlserver 中显示错误输出
- angular - 使用 Angular CLI 运行 ng new 和 ng serve 后显示空白屏幕
- c++ - 如何使用 sregex_token_iterator 对象初始化向量
- rust - Rust 迭代器上复制和克隆的区别
- function - 如何编写 MIPS 汇编代码来编写函数
- vue.js - Vuex 无法使用模块
- javascript - blazor(服务器端)javascript互操作:Javascript失败,因为代码必须以用户启动的方式运行
- regex - 单独的 json 键:用逗号表示值,但值引号内没有闭合引号
- c# - 使用 Lambda 过滤并更改一些值
- c# - 如何使用 C# 和 WPF 创建自定义 ListBoxItem?