如何使用python将XML转换为CSV文件?

时间:2019-06-11 18:34:12

标签: python xml csv

我想在cvs中编码该xml文档。我试过了,但不起作用,我不知道自己在做什么错。

有我要转换的xml

<?xml version="1.0" encoding="UTF-8"?> 
<Shot 
  Shotcode = "30AA" 
  ShotDate = "4/2/2000"> 

 <Images> 

  <Image 
   ImageNumber="103" 
   RawFileName="18_Shot_30AA.jpg" /> 
  <Image 
   ImageNumber="104"  
   RawFileName="17_Shot_30AA.jpg" /> 
  <Image 
   ImageNumber="105" 
   RawFileName="14_Shot_30AA" /> 
 </Images> 

 <Metrics> 
  <Metric 
   Name = "30AA" 
   TypeId = "163" 
   Value = "0" /> 

 <Metric 
  Name = "Area" 
  TypeId = "10" 
  Value = "63" /> 
 </Metrics> 

</Shot>

我以这种形式编写代码,以完成一些示例,但不是完整的程序,而是显示我在做什么。

import xml.etree.ElementTree as ET
import csv

tree = ET.parse("30AA.xml")
root = tree.getroot()

30AA = open('30AA.csv', 'w+')
csvwriter = csv.writer(30AA)
head = []

count = 0   #loops
for member in root.findall('Shot'):
Shot = []
if count == 0:
    ShotCode = member.find('ShotCode').tag
    head.append(ShotCode)
    ShotDate = member.find('ShotDate').tag
    head.append(ShotDate)
    csvwriter.writerow(head)
    count = count + 1   
ShotCode = member.find('ShotCode').txt
Shot.append(ShotCode)
ShotDate = member.find('ShotDate').txt
Shot.append(ShotDate)   
30AA.close()

我期望的结果是

Shotcode    30AA    
ShotDate    4/2/2000    

Imagen  103 

Imagen  104 

Imagen  105 

Name TypeId Value
30AA  163   0
area  10    63

1 个答案:

答案 0 :(得分:0)

好的,我想我发现出了什么问题,主要的问题主要是在读取xml时,看起来就像是csv一样。

您xml的根是Shot标记,因此您不能使用root.findall('Shot')来获取所有标记,因为root已经存在并且其中没有任何Shot。 因此,为什么您的输出中没有任何内容。

另外,当您想获取标签的属性时,请使用.attrib ['name_of_attribute'],例如,代替member.find('ShotCode')。tag应该是member.attrib ['ShotCode']

这会大大改变脚本的其余部分,但是您需要执行以下操作:

root = tree.getroot()

_30AA = open('30AA.csv', 'w+')
csvwriter = csv.writer(_30AA)
head = []

ShotCode = root.attrib['Shotcode']

csvwriter.writerow(['ShotCode', ShotCode])
head.append(ShotCode)
ShotDate = root.attrib['ShotDate']
csvwriter.writerow(['ShotDate', ShotDate])
# member is going to be the <Images> and <Metrics>
for member in root.getchildren():

    submembers = member.getchildren()
    # Write the names of the attributes as headings
    keys = submembers[0].attrib.keys()
    csvwriter.writerow(keys)
    for submember in submembers:

        row_data = [submember.attrib[k] for k in keys]
        csvwriter.writerow(row_data )

_30AA.close()

会给你你想要的东西