如何使用python将xml文件中的特定值转换为csv文件? [英] How to get specific values from a xml file into csv file using python?

查看:40
本文介绍了如何使用python将xml文件中的特定值转换为csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试提取每个对象标签的对象、xmin、ymin、xmax 和 xmax 值.

XML<注释><文件夹>板_编号</文件夹><文件名>1.png</文件名><来源><数据库>未知</数据库></来源><尺寸><宽度>294</宽度><高度>60</高度><depth>3</depth></尺寸><segmented>0</segmented><对象><名称>2</名称><pose>未指定</pose><截断的>1</截断的><困难>0</困难><xmin>40</xmin><ymin>1</ymin><xmax>69</xmax><ymax>42</ymax></bndbox></对象><对象><姓名>10</姓名><pose>未指定</pose><截断的>0</截断的><困难>0</困难><xmin>67</xmin><ymin>3</ymin><xmax>101</xmax><ymax>43</ymax></bndbox></对象><对象><姓名>1<pose>未指定</pose><截断的>0</截断的><困难>0</困难><xmin>122</xmin><ymin>2</ymin><xmax>153</xmax><ymax>45</ymax></bndbox></对象><对象><姓名>10</姓名><pose>未指定</pose><截断的>0</截断的><困难>0</困难><xmin>151</xmin><ymin>3</ymin><xmax>183</xmax><ymax>44</ymax></bndbox></对象><对象><名称>2</名称><pose>未指定</pose><截断的>0</截断的><困难>0</困难><xmin>186</xmin><ymin>4</ymin><xmax>216</xmax><ymax>47</ymax></bndbox></对象><对象><姓名>5</姓名><pose>未指定</pose><截断的>0</截断的><困难>0</困难><xmin>214</xmin><ymin>5</ymin><xmax>245</xmax><ymax>46</ymax></bndbox></对象></注解>

这是我尝试过但没有得到预期结果

蟒蛇导入 xml.etree.ElementTree 作为 ET导入 csv树 = ET.parse("1.xml")root = tree.getroot()# 打开一个文件进行写入data = open('test.csv', 'r+')# 创建 csv writer 对象csvwriter = csv.writer(数据)数据头 = []计数 = 0对于 root.findall('object') 中的成员:对象 = []bndbox_list = []如果计数== 0:name = member.find('name').tagdata_head.append(name)bndbox = 成员[4].tagdata_head.append(bndbox)csvwriter.writerow(data_head)计数 = 计数 + 1name = member.find('name').textobj.append(name)bndbox = 成员[4][0].textbndbox_list.append(bndbox)xmin = 成员[4][1].textbndbox_list.append(xmin)ymin = 成员[4][2].textbndbox_list.append(ymin)xmax = 成员[4][3].textbndbox_list.append(xmax)ymax = 成员[4][4].textbndbox_list.append(ymax)obj.append(bndbox)csvwriter.writerow(数据)数据关闭()

我期待名称 xmin ymin xmax ymax2 40 1 69 4210 67 3 101 431 122 2 153 4510 151 3 183 442 186 4 216 475 214 5 245 46

但我只得到这两个标题

名称绑定框

没有价值

解决方案

如果可以使用 BeautifulSoup,你可以使用

from bs4 import BeautifulSoup汤 = BeautifulSoup(input_xml_string)tgs = 汤.find_all('对象', 'xml')l = [(i.find('name').string, i.xmin.string, i.ymin.string, i.xmax.string, i.ymax.string) for i in tgs]

其中 input_xml_string 是字符串形式的输入 xml.

soup 将是 BeautifulSoup 对象,它是 xml 树的表示.

使用 xml 解析器.

然后使用find_all()函数查找xml中所有的标签.结果存储在tgs中.

现在从 tgs 中的元素,它们将是 的子标签,我们选择我们需要的标签,它们是 Tag 对象,并使用它们的 获取它们的值字符串 属性.

我们可以使用 string 属性访问 name 中的值,但 name 的属性名称标签类.因此,我们首先使用 find() 来获取 子元素,然后我们获取其内容.>

现在如果我们打印 l 中的值,

for i in l:打印(一)

我们会得到,

('2', '40', '1', '69', '42')('10', '67', '3', '101', '43')('1', '122', '2', '153', '45')('10', '151', '3', '183', '44')('2', '186', '4', '216', '47')('5', '214', '5', '245', '46')

I am trying to extract object, xmin, ymin, xmax and xmax value of every object tag there is.

XML

<annotation>
    <folder>Plates_Number</folder>
    <filename>1.png</filename>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>294</width>
        <height>60</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>2</name>
        <pose>Unspecified</pose>
        <truncated>1</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>40</xmin>
            <ymin>1</ymin>
            <xmax>69</xmax>
            <ymax>42</ymax>
        </bndbox>
    </object>
    <object>
        <name>10</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>67</xmin>
            <ymin>3</ymin>
            <xmax>101</xmax>
            <ymax>43</ymax>
        </bndbox>
    </object>
    <object>
        <name>1</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>122</xmin>
            <ymin>2</ymin>
            <xmax>153</xmax>
            <ymax>45</ymax>
        </bndbox>
    </object>
    <object>
        <name>10</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>151</xmin>
            <ymin>3</ymin>
            <xmax>183</xmax>
            <ymax>44</ymax>
        </bndbox>
    </object>
    <object>
        <name>2</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>186</xmin>
            <ymin>4</ymin>
            <xmax>216</xmax>
            <ymax>47</ymax>
        </bndbox>
    </object>
    <object>
        <name>5</name>
        <pose>Unspecified</pose>
        <truncated>0</truncated>
        <difficult>0</difficult>
        <bndbox>
            <xmin>214</xmin>
            <ymin>5</ymin>
            <xmax>245</xmax>
            <ymax>46</ymax>
        </bndbox>
    </object>
</annotation>

This is what I tried but didn't get the expected result

python

import xml.etree.ElementTree as ET
import csv

tree = ET.parse("1.xml")
root = tree.getroot()

# open a file for writing

data = open('test.csv', 'r+')

# create the csv writer object

csvwriter = csv.writer(data)
data_head = []

count = 0
for member in root.findall('object'):
    obj = []
    bndbox_list = []
    if count == 0:
        name = member.find('name').tag
        data_head.append(name)
        bndbox = member[4].tag
        data_head.append(bndbox)
        csvwriter.writerow(data_head)
        count = count + 1

    name = member.find('name').text
    obj.append(name)
    bndbox = member[4][0].text
    bndbox_list.append(bndbox)
    xmin = member[4][1].text
    bndbox_list.append(xmin)
    ymin = member[4][2].text
    bndbox_list.append(ymin)
    xmax = member[4][3].text
    bndbox_list.append(xmax)
    ymax = member[4][4].text
    bndbox_list.append(ymax)
    obj.append(bndbox)
    csvwriter.writerow(data)
data.close()

I expect Name xmin ymin xmax ymax 2 40 1 69 42 10 67 3 101 43 1 122 2 153 45 10 151 3 183 44 2 186 4 216 47 5 214 5 245 46

but I am only getting these two header

Name bndbox

and no value

解决方案

If you can use BeautifulSoup, you could use

from bs4 import BeautifulSoup
soup = BeautifulSoup(input_xml_string)
tgs = soup.find_all('object', 'xml')
l = [(i.find('name').string, i.xmin.string, i.ymin.string, i.xmax.string, i.ymax.string) for i in tgs]

where input_xml_string is the input xml in string form.

soup would be a BeautifulSoup object which is a representation of the xml tree.

An xml parser is used.

Then the find_all() function is used to find all the <object> tags in the xml. The result is stored in tgs.

Now from the elements in tgs, which would be children tags of <object>, we select the tags we need, which are Tag objects, and get their values using their string attribute.

We could have accessed the value in name using its string attribute but name is the name of an attribute of the Tag class. So we first used find() to get the <name> child of <object> and then we got its content.

Now if we print the values in l,

for i in l:
    print(i)

we would get,

('2', '40', '1', '69', '42')
('10', '67', '3', '101', '43')
('1', '122', '2', '153', '45')
('10', '151', '3', '183', '44')
('2', '186', '4', '216', '47')
('5', '214', '5', '245', '46')

这篇关于如何使用python将xml文件中的特定值转换为csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆