python xml转excel_一个python 脚本将XML文件转换到excel

python xml转excel_一个python 脚本将XML文件转换到excel需要下载一个module:xlwt,如下是sourcecodeimportxml.dom.minidomimportxlwtimportsyscol=0row=0defhandle_xml_report(xml_report,excel):problems=xml_report.getElementsByTagName(“problem”)handle_problems(p…

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

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

需要下载一个module:xlwt,如下是source code

import xml.dom.minidom

import xlwt

import sys

col = 0

row = 0

def handle_xml_report(xml_report, excel):

problems = xml_report.getElementsByTagName(“problem”)

handle_problems(problems, excel)

def handle_problems(problems, excel):

for problem in problems:

handle_problem(problem, excel)

def handle_problem(problem, excel):

global row

global col

code = problem.getElementsByTagName(“code”)

file = problem.getElementsByTagName(“file”)

line = problem.getElementsByTagName(“line”)

message   = problem.getElementsByTagName(“message”)

for node in code:

excel.write(row, col, node.firstChild.data)

col = col + 1

for node in file:

excel.write(row, col, node.firstChild.data)

col = col + 1

for node in line:

excel.write(row, col, node.firstChild.data)

col = col + 1

for node in message:

excel.write(row, col, node.firstChild.data)

col = col + 1

row = row+1

col = 0

if __name__ == ‘__main__’:     if(len(sys.argv) <= 1):         print (“usage: xml2xls src_file [dst_file]”)         exit(0)     #the 1st argument is XML report ; the 2nd is XLS report     if(len(sys.argv) == 2):         xls_report = sys.argv[1][:-3] + ‘xls’     #if there are more than 2 arguments, only the 1st & 2nd make sense     else:         xls_report = sys.argv[2]     xmldoc = xml.dom.minidom.parse(sys.argv[1])     wb = xlwt.Workbook()     ws = wb.add_sheet(‘MOLint’)     ws.write(row, col, ‘Error Code’)     col = col + 1     ws.write(row, col, ‘file’)     col = col + 1        ws.write(row, col, ‘line’)        col = col + 1        ws.write(row, col, ‘Description’)     row = row + 1     col = 0     handle_xml_report(xmldoc, ws)     wb.save(xls_report)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • android ListView 嵌套 ListView

    android ListView 嵌套 ListView实现的效果是这个样子的看上去效果还是不错,不过现在有个刷新问题一直没能解决,刷新的时候里面的adapter进行刷新的时候总是会让里面的listview消失掉,应该是父listview先刷新完后,子listview还未刷新完成,导致测量的高度不对,就会消失,像当前组已关闭这种,现在这个问题还没有想到办法解决的,试过比较多的方法,添加接口让子listview刷新完成后再去更新父…

    2022年7月16日
    20
  • php源码审计_代码审计入门cms

    php源码审计_代码审计入门cms目录一:代码审计的定义二:为什么选择PHP学习代码审计三:入门准备四:PHP常见的套路4.1 代码结构4.2 目录结构4.3参考项目五:如何调试代码六:代码审计的本质一:代码审计的定义通过阅读一些程序的源码去发现潜在的漏洞,比如代码不规范,算法性能不够,代码重用性不强以及其他的缺陷等等从安全人员的角度来看是:查找代码中是否存在安全问题,推断用户在操…

    2022年10月1日
    0
  • winrar 去广告 使用 hxd 或 任意二进制修改器

    winrar 去广告 使用 hxd 或 任意二进制修改器不喜使用winrar,但是winrar解压算法偶尔会更新,其他解压缩软件没有及时更新,就会造成只能用winrar来解压的情况参考方法来自https://www.52pojie.cn/thread-648133-1-1.html只是觉得用ResHacker太麻烦“##0aN9…”这串字符串是以宽字符串为保存的,如果要用搜索字符串方法,需要用搜索unicode字符串…

    2022年5月27日
    58
  • 计算机-底层-1

    计算机-底层-1

    2021年5月18日
    106
  • log4j.properties 详解与配置步骤[通俗易懂]

    log4j.properties 详解与配置步骤[通俗易懂]一、入门实例1.新建一个JAva工程,导入包log4j-1.2.17.jar,整个工程最终目录如下2、src同级创建并设置log4j.properties###设置###log4j.rootLogger=debug,stdout,D,E###输出信息到控制抬###log4j.appender.stdout=org.apache.log4j.ConsoleAp…

    2022年9月30日
    1
  • JAVA的HelloWorld代码编写

    JAVA的HelloWorld代码编写第一步:新建一个文本文档第二步:打开代码输入代码(注意大小写,Java对大小写很敏感)第三步:更改后缀为.java(这样这个文档就会成为一个原文件)第四步:按住shift键,鼠标右键单击,点击“在此处打开Powershell”第五步:在Powershell窗口里输入JavacHelloworld.java,会出现一个class文档第六步:在Powershell窗口里输入JavacHelloworld会输出HelloWorld…

    2022年5月8日
    452

发表回复

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

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