python代码相关语法

1. python中用print()输出多个格式化参数

print("有两个不同的解: %.2f,%.2f"%(root1,root2)) #保留2位小数

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)

今天身体不舒服,没有更新新的内容,但是为了日更不断,凑字数啊凑字数!!!回头再进行补上!
嘿嘿!
今天身体不舒服,没有更新新的内容,但是为了日更不断,凑字数啊凑字数!!!回头再进行补上!
嘿嘿!
今天身体不舒服,没有更新新的内容,但是为了日更不断,凑字数啊凑字数!!!回头再进行补上!
嘿嘿!
今天身体不舒服,没有更新新的内容,但是为了日更不断,凑字数啊凑字数!!!回头再进行补上!
嘿嘿!
今天身体不舒服,没有更新新的内容,但是为了日更不断,凑字数啊凑字数!!!回头再进行补上!
嘿嘿!
重要的事情说五遍啦!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容