#coding=utf-8
##此文件放在要写入文件的文件夹里
import glob
import re
aa=[]
aa=glob.glob(r'D:\sales\all_label\*.xml')#读入所有的xml文件,得到a列表
print(aa) #aa列表存储所有xml文件名
for a in aa:
with open(a, 'r', encoding='UTF-8') as fo:
contents = fo.readlines() #把所有行,存进contents列表里
for content in contents:#找宽和高
width=re.search('<width>(.*)</width>',content)
if width:#如果非空
w=int(width.group(1)) #宽度
height=re.search('<height>(.*)</height>',content)
if height:
h=int(height.group(1)) #高度
break #找完高度跳出循环
for content in contents: #content是一行的字符串
xmin=re.search('<xmin>(.*)</xmin>', content)
if xmin:#如果非空
content = re.sub('\d+',str(w-int(xmin.group(1))), content)#\d+代表替换掉数字
xmax=re.search('<xmax>(.*)</xmax>', content)
if xmax:#如果非空
content = re.sub('\d+',str(w-int(xmax.group(1))), content)
#下面是逐行写入
newname=a.lstrip("D:\\sales\\all_label\\") #去掉前缀,不会改变a自身
with open('11_'+newname, 'a', encoding='UTF-8') as fs:
fs.write(content)
python对标注的坐标进行扩展
最新推荐文章于 2024-06-17 12:31:47 发布
