Python 解析 XML 文件生成 HTML
XML文件result.xml,内容如下:
<ccm>
<metric>
<complexity>1</complexity>
<unit>multiply</unit>
<classification>A</classification>
<file>all\mymath.py</file>
<startLineNumber>9</startLineNumber>
<endLineNumber>10</endLineNumber>
</metric>
<metric>
<complexity>1</complexity>
<unit>divide</unit>
<classification>A</classification>
<file>all\mymath.py</file>
<startLineNumber>13</startLineNumber>
<endLineNumber>14</endLineNumber>
</metric>
</ccm>
import xml.etree.cElementTree as ET
import os
import sys
tree = ET.ElementTree(file='result.xml')
# 根元素(root)是一个Element对象。我们看看根元素都有哪些属性
root = tree.getroot()
# 没错,根元素并没有属性。与其他Element对象一样,根元素也具备遍历其直接子元素的接口
for child_of_root in root:
print(child_of_root,child_of_root.attrib)
for x in child_of_root:
print(child_of_root, x, x.tag,':',x.text)
利用Jinja2生成HTML
模版文件templa/base.html:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Radon</title>
<link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<h1>Radon-圈复杂度检查结果</h1>
<table class="table table-hover">
<thead>
<tr>
{% for td in data[0] %}
<th>{{ td.tag }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{%for m in data%}
{% set complexity = m[0].text|float %}
{% if m[0].text|float < 6 %} #或者 {% if complexity < 6 %}
<tr class="success">
{% for v in m %}
<td>{{v.text}}</td>
{% endfor %}
</tr>
{% else %}
<tr class="danger">
{% for v in m %}
<td>{{v.text}}</td>
{% endfor %}
</tr>
{% endif %}
{%endfor%}
</tbody>
</table>
</body>
</html>
渲染脚本:
from jinja2 import Environment, FileSystemLoader
t=[]
for metric in root:
t.append(metric)
print(t)
xml_loader = FileSystemLoader("template")
xml_env = Environment(loader=xml_loader)
xml_tmp = xml_env.get_template("base.html")
xml_info = xml_tmp.render(data=t)
with open(os.path.join("template", "result.html"), "w") as f:
f.write(xml_info)
参考:
- 深入解读Python解析XML的几种方式
- xml.etree.ElementTree — The ElementTree XML API
- Does the Jinja2 templating language have the concept of 'here' (current directory)?
- 欢迎来到 Jinja2
- jinja2模板
Python 解析 XML 文件生成 HTML的更多相关文章
- 【TensorFlow】Python解析xml文件
最近在项目中使用TensorFlow训练目标检测模型,在制作自己的数据集时使用了labelimg软件对图片进行标注,产生了VOC格式的数据,但标注生成的xml文件标签值难免会产生个别错误造成程序无法跑 ...
- python 解析xml 文件: Element Tree 方式
环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...
- python 解析xml 文件: DOM 方式
环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...
- python 解析xml 文件: SAX方式
环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...
- 遍历文件 创建XML对象 方法 python解析XML文件 提取坐标计存入文件
XML文件??? xml即可扩展标记语言,它可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言. 里面的标签都是可以随心所欲的按照他的命名规则来定义的,文件名为roi.xm ...
- Python解析xml文件遇到的编码解析的问题
使用python对xml文件进行解析的时候,假设xml文件的头文件是utf-8格式的编码,那么解析是ok的,但假设是其它格式将会出现例如以下异常: xml.parsers.expat.ExpatErr ...
- python 解析 XML文件
如下使用xml.etree.ElementTree模块来解析XML文件.ElementTree模块中提供了两个类用来完成这个目的: ElementTree表示整个XML文件(一个树形结构) Eleme ...
- [转载] python 解析xml 文件: SAX方式
环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...
- python解析xml文件时使用ElementTree和cElementTree的不同点;iter
在python中,解析xml文件时,会选用ElementTree或者cElementTree,那么两者有什么不同呢? 1.cElementTree速度上要比ElementTree快,比较cElemen ...
随机推荐
- Docker日志
搜索Redis镜像 docker search redis ***************************** 拉取Redis镜像 docker pull redis ************ ...
- quartz demo01
1,Pom.xml 加入:quartz-2.1.7.jar <dependency> <groupId>org.quartz-scheduler</groupId&g ...
- uvm设计分析——report
uvm_report实现中的类图,如下: 1)uvm_component均从uvm_report_object extend而来,其中定义了report_warning,error,info,fata ...
- 三 js语句
/** * Created by Administrator on 2017/12/24. * 1.顺序语句 * 2.选择语句 if else swtich case * 3.循环语句 for whi ...
- 找不到System.Web.Optimization命名空间
找不到System.Web.Optimization命名空间,无法完成BundleConfig.cs内容的添加. 解决方法如下:打开程序包管理控制台,在控制台中输入:Install-PackageMi ...
- CNN那么多的网络有什么区别吗?如何对CNN网络进行修改?
https://www.zhihu.com/question/53727257/answer/136261195 http://blog.csdn.net/csmqq/article/details/ ...
- python_super注意事项
class room: def __init__(self,area=120,usedfor='sleep'): self.area = area self.usedfor = usedfor def ...
- C# 声明隐式类型的局部变量
在c#中赋值给变量的值必须具有和变量相同的类型.如int值赋给int变量,c#编译器可以迅速判断变量初始化表达式的类型,如果变量类型不符,就会明确告诉你. 提示需要强制转换(例如在char中不允许使用 ...
- 翻唱 - shape of you - 个个版本
翻唱: http://7j1xky.com1.z0.glb.clouddn.com/1525514286196.mp4 乐队版-我的翻唱-混合 http://7j1xky.com1.z0.glb.cl ...
- 登陆 全站 user
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join( ...