python解析xml实例
如下,一个银行卡打标签后导出的数据
<?xml version="1.0" encoding="ISO-8859-1"?>
<annotation>
<filename>a001.jpg</filename>
<folder>users/three33//card</folder>
<source>
<submittedBy>three</submittedBy>
</source>
<imagesize>
<nrows>2240</nrows>
<ncols>3968</ncols>
</imagesize>
<object>
<name>numbers</name>
<deleted>0</deleted>
<verified>0</verified>
<occluded>no</occluded>
<attributes>6228480808055442079</attributes>
<parts>
<hasparts/>
<ispartof/>
</parts>
<date>12-May-2019 06:21:39</date>
<id>0</id>
<type>bounding_box</type>
<polygon>
<username>anonymous</username>
<pt>
<x>927</x>
<y>1278</y>
</pt>
<pt>
<x>3269</x>
<y>1278</y>
</pt>
<pt>
<x>3269</x>
<y>1475</y>
</pt>
<pt>
<x>927</x>
<y>1475</y>
</pt>
</polygon>
</object>
</annotation>
(上面的代码无法保留格式,还是截张图吧

现要将其中的标记的四个坐标和银行卡号读取出来,并保存到文本文件。由于有几百张图片,需要批处理。
代码:
import os
import sys
import xml.etree.cElementTree as ET from_path = "./card" //输入文件夹
to_path = "./cardout" //输出文件夹
files = os.listdir(from_path)
files.sort() #按字典序排序 i = 1
for filename in files: dir1 = os.path.join(from_path, filename)
tree = ET.ElementTree(file=dir1)
root = tree.getroot() new_filename = filename[:-4] + ".txt"
dir2 = os.path.join(to_path,new_filename) fobj = open(dir2,'w+') print("time: %d, from_filename: %s, to_filename: %s" % (i, dir1, dir2)) for elem in tree.iterfind('object/polygon/pt'):
fobj.write((elem[0].text + ',' + elem[1].text + ','))
#print(elem[0].text + ',' + elem[1].text + ',') for elem in tree.iterfind('object/attributes'):
fobj.write(elem.text) fobj.close()
i = i + 1
效果:

python解析xml实例的更多相关文章
- Python 解析XML实例(xml.sax)
已知movies.xml <collection shelf="New Arrivals"> <movie title="Enemy Behind&qu ...
- python 解析XML python模块xml.dom解析xml实例代码
分享下python中使用模块xml.dom解析xml文件的实例代码,学习下python解析xml文件的方法. 原文转自:http://www.jbxue.com/article/16587.html ...
- python解析xml模块封装代码
在python中解析xml文件的模块用法,以及对模块封装的方法.原文转自:http://www.jbxue.com/article/16586.html 有如下的xml文件:<?xml vers ...
- python解析xml之lxml
虽然python解析xml的库很多,但是,由于lxml在底层是用C语言实现的,所以lxml在速度上有明显优势.除了速度上的优势,lxml在使用方面,易用性也非常好.这里将以下面的xml数据为例,介绍l ...
- python解析xml
python解析xml import xml.dom.minidom as minidom dom = minidom.parse("aa.xml") root = dom.get ...
- python 解析xml
在工作中很多时候都要用到xml,使用这个时候难免会设计到解析他,然后就研究了一下python解析xml问题,看了很多东西,python有很多解析xml的包,但是也折腾我好一段时间,最后选择了这个方法. ...
- Python 解析 XML 文件生成 HTML
XML文件result.xml,内容如下: <ccm> <metric> <complexity>1</complexity> <unit> ...
- 横向对比分析Python解析XML的四种方式
横向对比分析Python解析XML的四种方式 在最初学习PYTHON的时候,只知道有DOM和SAX两种解析方法,但是其效率都不够理想,由于需要处理的文件数量太大,这两种方式耗时太高无法接受. 在网络搜 ...
- python 解析xml 文件: SAX方式
环境 python:3.4.4 准备xml文件 首先新建一个xml文件,countries.xml.内容是在python官网上看到的. <?xml version="1.0" ...
随机推荐
- 在windows10环境下给PHPStorm配置xdebug断点调试功能
笔者的开发环境: wampserver2.5系统环境包 操作系统:windows10 64位专业版. php版本:php5.512 Apache版本:2.49 好了下面介绍,phpstorm配置xde ...
- Linux做脚本定时任务(定时清理日志)
无论一些面试问题,还是实际应用,都会用到虚拟机的定时任务.现做定时清理日志日志做一总结. 1.查看/etc/crontab文件. linux 系统则是由 cron (crond) 这个系统服务来控制的 ...
- The web.config file for this project is missing the required DirectRequestModule.
The web.config file for this project is missing the required DirectRequestModule. 将应用程序集的模式由集成改为经典 ...
- Java中的Synchronized关键字用法
认识synchronized 对于写多线程程序的人来说,经常碰到的就是并发问题,对于容易出现并发问题的地方加上synchronized修饰符基本上就搞定 了,如果说不考虑性能问题的话,这一招绝对能应对 ...
- AnimationCurve
http://blog.sina.com.cn/s/blog_471132920101f8nv.html 说明:关于animationCurve的使用总结1 创建,可以新建脚本,创建animation ...
- json 打印
JsonObject jsonObj = new JSONObject(); jsonObj.put("success",true); jsonObj.put("msg& ...
- ios 适配问题
两张图解决
- 编译最新linux内核(version 4.4.2)
环境:centos6.4 内核版本为2.6.32 目标:编译4.4.2内核,升级到 4.4.2 准备工作: 安装开发库和ncurses库 # yum groupinstall "Develo ...
- Programming Ruby 阅读笔记
在Ruby中,通过调用构造函数(constructor)来创建对象 song1=Song.new("Ruby") Ruby对单引号串处理的很少,除了极少的一些例外,键入到字符串字面 ...
- angularJs 指令调用父controller某个方法
1.父级controller:例如有个 init() 方法; 父级与子级的通信数据是$scope.controlFlag={}; 那么可以在父级controller里这样写:$scope.contro ...