1. python中用print()輸出多個格式化參數(shù)
print("有兩個不同的解: %.2f,%.2f"%(root1,root2)) #保留2位小數(shù)
2. python讀取XML文件
- XML文件示例:
<annotation>
<folder>Desktop</folder>
<filename>2.0.jpg</filename>
<source>
<database>My Database</database>
</source>
<size>
<width>1280</width>
<height>720</height>
<depth>3</depth>
</size>
<segmented>0</segmented>
<object>
<name>Tool31</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>555</xmin>
<ymin>248</ymin>
<xmax>642</xmax>
<ymax>436</ymax>
</bndbox>
</object>
<object>
<name>Tool31</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>286</xmin>
<ymin>359</ymin>
<xmax>587</xmax>
<ymax>555</ymax>
</bndbox>
</object>
<object>
<name>Tool31</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>578</xmin>
<ymin>531</ymin>
<xmax>1273</xmax>
<ymax>670</ymax>
</bndbox>
</object>
<object>
<name>Tool2</name>
<pose>Unspecified</pose>
<truncated>0</truncated>
<difficult>0</difficult>
<bndbox>
<xmin>848</xmin>
<ymin>320</ymin>
<xmax>1273</xmax>
<ymax>522</ymax>
</bndbox>
</object>
</annotation>
- 讀取XML
import os
import xml.etree.ElementTree as ET
FilePath="E:\\video\XML\Frame4\\"
FileLists=os.listdir(FilePath)
f=open('position.txt','a')
for file in FileLists:
filePath=FilePath+file
#print(file)
tree=ET.parse(filePath)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
#print(w,h)
for obj in root.iter('object'):
cls = obj.find('name').text
xmlbox = obj.find('bndbox')
xmin=float(xmlbox.find('xmin').text)
xmax = float(xmlbox.find('xmax').text)
ymin = float(xmlbox.find('ymin').text)
ymax = float(xmlbox.find('ymax').text)
xcenter=(xmin+xmax)/2
ycenter = (ymin + ymax) / 2
#print(file)
#print(cls)
if cls==None:
print(file)
#print(xcenter)
f.write(cls+' '+str(xcenter)+' '+str(ycenter)+'\n')
#print(cls,xmin,xmax,ymin,ymax,xcenter,ycenter)
- 修改XML
import os
import xml.etree.ElementTree as ET
FilePath="E:\\video\XML\Frame11\\"
FileLists=os.listdir(FilePath)
for file in FileLists:
filepath=FilePath+file
tree=ET.parse(filepath)
root = tree.getroot()
for obj in root.iter('object'):
name = obj.find('name')
cls = obj.find('name').text
if cls == "Tool9":
print(cls)
name.text = "Tool8"
tree.write(filepath)
今天身體不舒服弟跑,沒有更新新的內(nèi)容雕欺,但是為了日更不斷著蛙,湊字數(shù)啊湊字數(shù):萄洹!孽拷!回頭再進行補上斟湃!
嘿嘿联予!
今天身體不舒服,沒有更新新的內(nèi)容率翅,但是為了日更不斷练俐,湊字數(shù)啊湊字數(shù)!C岢簟腺晾!回頭再進行補上!
嘿嘿辜贵!
今天身體不舒服悯蝉,沒有更新新的內(nèi)容,但是為了日更不斷托慨,湊字數(shù)啊湊字數(shù)1怯伞!厚棵!回頭再進行補上嗡靡!
嘿嘿!
今天身體不舒服窟感,沒有更新新的內(nèi)容,但是為了日更不斷歉井,湊字數(shù)啊湊字數(shù)J疗怼!哩至!回頭再進行補上躏嚎!
嘿嘿!
今天身體不舒服菩貌,沒有更新新的內(nèi)容卢佣,但是為了日更不斷,湊字數(shù)啊湊字數(shù)<住P椴琛!回頭再進行補上仇参!
嘿嘿嘹叫!
重要的事情說五遍啦!