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

Python 奇淫技巧 — 利用pandas读取xml转换为excel因为工作需要,将xml中特定的节点值取出来,然后统计到excel中。于是乎试试写了一个python脚本,加快工作效率。而且今后还能复用。以下为完整示例,需要的朋友们可参考。示例XML<?xmlversion=”1.0″encoding=”utf-8″?><breakfast_menu><food><name>BelgianWaffles</name><price>$5.95

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

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

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

示例 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>  
  <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 脚本

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.net/171956.html原文链接:https://javaforall.net

(0)
上一篇 2022年8月22日 下午5:00
下一篇 2022年8月22日 下午5:00


相关推荐

  • vmware2021 .7最新激活码【2021最新】

    (vmware2021 .7最新激活码)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.htmlMLZPB5EL5Q-eyJsaWN…

    2022年3月21日
    52
  • C# License

    C# License第一步:生成一组公钥和私钥,公钥用于你发布程序,私钥属于注册码生成。using(RSACryptoServiceProviderrsa=new RSACryptoServiceProvider())  {  stringpubkey=rsa.ToXmlString(false);  //公钥 stringprikey=rsa.ToXmlString(t

    2022年7月26日
    19
  • ajax菜鸟教程html,菜鸟教程–AJAX[通俗易懂]

    一.简介1.AJAX是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。2.AJAX=异步JavaScript和XML。3.通过在后台与服务器进行少量数据交换,AJAX可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。二.原理:1.创建XMLHttpRequest对象(1).XMLHttpRequest对象:所有现代浏览器均支…

    2022年4月8日
    85
  • 关于内存地址的一些理解

    关于内存地址的一些理解首先 必须要知道内存地址只是一个编号 如 1000H 代表一个内存空间 在计算机中存储器的容量是以字节为基本单位的 也就是说一个内存地址代表一个字节 8bit 的存储空间 例如经常说 32 位的操作系统最多支持 4GB 的内存空间 也就是说 CPU 只能寻址 2 的 32 次方 4GB 注意这里的 4GB 是以 Byte 为单位的 不是 bit 也就是说有 4G 4 1024M Byte 4 1024 1024Kb Byt

    2026年3月17日
    1
  • 重复字符串 leetcode_字符串中出现最多的子串 leetcode

    重复字符串 leetcode_字符串中出现最多的子串 leetcode原题链接给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。示例 1:输入: s = “abcabcbb”输出: 3 解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。示例 2:输入: s = “bbbbb”输出: 1解释: 因为无重复字符的最长子串是 “b”,所以其长度为 1。示例 3:输入: s = “pwwkew”输出: 3解释: 因为无重复字符的最长子串是 “wke”,所以其长度为 3。 请注意,你的答案必须是 子串 的长度,”pwk

    2022年8月8日
    10
  • VHDL 与 VerilogHDL 详细对比

    VHDL 与 VerilogHDL 详细对比序号区别之处VHDLVerilog1文件的扩展名不一样.vhd.v2结构不一样包含库、实体、结构体。ENTITY实体名ISPORT(端口说明)END实体名;ARCHITECTU…

    2026年2月12日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号