我要复制如下所示的.txt文件的内容(它是图像的标签和像素值):
2
33 45 77 90
如何使用Python将其转换为.xml文件?
编辑:
我的abc.txt文件如下:
1
76 45 146 87
这是我在xyz.xml文件中显示的预期输出:
-<root>
-<object>
- <label>1</label>
</object>
-<cordinates>
<xmin>76</xmin>
<ymin>45</ymin>
<xmin>146</xmin>
<xmax>87</xmax>
</cordinates>
</root>
这是源代码:
import xml.etree.cElementTree as ET
root = ET.Element("root")
object = ET.SubElement(root, "object")
ET.SubElement(object, "label").text = "1"
cordinates = ET.SubElement(root, "cordinates")
ET.SubElement(cordinates, "xmin").text = "76"
ET.SubElement(cordinates, "ymin").text = "45"
ET.SubElement(cordinates, "xmin").text = "146"
ET.SubElement(cordinates, "xmax").text = "87"
tree = ET.ElementTree(root)
tree.write("xyz.xml")
Ps:但是问题是,我已经完成了标注并手动放置了坐标。我想在该程序中提取abc.txt文件,并自动对350多个.txt文件进行处理。
有人可以帮我重写代码吗?