python学习笔记4——解析xml、文件操作
2015-08-20 11:35
603 查看
最近在做船只检测方面的事情,需要大量的负样本来训练adaboost分类器。我从网上下载到一个pascal_voc的数据集,需要找到不包含船只的那些复制出来。
之前用c#写了一个
现在联系用python练习一下
相关练习
之前用c#写了一个
现在联系用python练习一下
import glob,os,xml.etree.cElementTree as ET,shutil fatherDir = r'E:\迅雷下载\VOCtrainval_11-May-2012\VOCdevkit\VOC2012' for filePath in glob.glob(fatherDir+r'\Annotations\*.xml'): fileName=os.path.split(filePath)[-1][:-4]#文件名; hasBoat=False for event, elem in ET.iterparse(filePath): if event == 'end': if elem.tag == 'name' and elem.text == 'boat': hasBoat=True elem.clear() break; elem.clear() # discard the element if hasBoat==False: shutil.copyfile(fatherDir+r'\JPEGImages'+'\\'+fileName+'.jpg',r'D:\IP_CV_WorkSpace\Img\NegSample2'+'\\'+fileName+'.jpg') else: hasBoat==False
相关练习
#测试处理xml文件 import xml.etree.cElementTree as ET count = 0 for event, elem in ET.iterparse('E:/迅雷下载/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/Annotations/2007_000032.xml'): if event == 'end': if elem.tag == 'object/name' and elem.text == 'person': count += 1 elem.clear() # discard the element print(count) 获取文件列表 import os print(os.listdir('E:/迅雷下载/VOCtrainval_11-May-2012/VOCdevkit/VOC2012/Annotations/')) import glob,os for filename in glob.glob(r'E:\迅雷下载\VOCtrainval_11-May-2012\VOCdevkit\VOC2012\Annotations\*.xml'): print(os.path.split(filename)[-1][:-4])#文件名; 下面的程序用来复制文件; import shutil shutil.copyfile(r'D:\IP_CV_WorkSpace\Img\TestBoatResult2\132264_120.jpg', r'D:\1.jpg')
相关文章推荐
- 第九章、通过表单和登录抓取
- The bytes/str dichotomy in Python 3
- python 文件操作
- /usr/bin/python^M: bad interpreter: No such file or directory
- python正则表达式
- python dir __dict__
- 待玩 python游戏
- 使用Python求解最大公约数的实现方法
- 欢迎使用CSDN-markdown编辑器
- 每日python(3)
- python基础语法
- Python 之 元组小结
- Python条件语句
- ubuntu14.04下配置python环境
- Python正则表达式指南
- ubuntu下安装Python的MySql的开发环境
- 关于python中urllib.urlencode的时候出错:UnicodeEncodeError: ‘ascii’的记录
- 使用Python3编写抓取网页和只抓网页图片的脚本
- Python有用的命令
- Python朝花夕拾