前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 奇淫技巧 — 利用pandas读取xml转换为excel

Python 奇淫技巧 — 利用pandas读取xml转换为excel

作者头像
全栈程序员站长
发布2022-09-23 09:59:34
1.7K0
发布2022-09-23 09:59:34
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

因为工作需要, 将xml中特定的节点值取出来, 然后统计到excel中。 于是乎试试写了一个python脚本, 加快工作效率。 而且今后还能复用。

以下为完整示例, 需要的朋友们可参考。

示例 XML

代码语言:javascript
复制
<?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>  
  <food> 
    <name>Berry-Berry Belgian Waffles</name>  
    <price>$8.95</price>  
    <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>  
    <calories>900</calories> 
  </food>  
  <food> 
    <name>French Toast</name>  
    <price>$4.50</price>  
    <description>Thick slices made from our homemade sourdough bread</description>  
    <calories>600</calories> 
  </food>  
  <food> 
    <name>Homestyle Breakfast</name>  
    <price>$6.95</price>  
    <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>  
    <calories>950</calories> 
  </food> 
</breakfast_menu>

python 脚本

代码语言:javascript
复制
from lxml import etree
import pandas as pd

def read_data_from_xml(xml_path):
    xml_content = ""
    with open(xml_path,'rb') as f:
        xml_content = f.read()

    excel_data = [["食物", "价格", "卡路里", "描述"]]

    xml_data = etree.XML(xml_content)
    foods = xml_data.xpath("//food")
    for food in foods:
        excel_row_data = []
        excel_row_data.extend(food.xpath("name/text()"))
        excel_row_data.extend(food.xpath("price/text()"))
        excel_row_data.extend(food.xpath("calories/text()"))
        excel_row_data.extend(food.xpath("description/text()"))
        excel_data.append(excel_row_data)

    return excel_data




def to_csv(writer, excel_data, sheet_name):

    data_df = pd.DataFrame(excel_data[1:])
    data_df.columns = excel_data[0]
    data_df.to_excel(writer,float_format='%.10f',index=False, sheet_name=sheet_name)
    worksheet = writer.sheets[sheet_name]
 	
 	// 设置列宽
    cols = "%s:%s" % ('A', chr(ord('A') + len(data_df.columns) - 1))
    worksheet.set_column(cols, 30)

// 读取xml
excel_data_ = read_data_from_xml("food.xml")

writer = pd.ExcelWriter('food.xlsx')
to_csv(writer, excel_data_, "food1")
writer.save()

最终效果

在这里插入图片描述
在这里插入图片描述

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/171956.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档