Python在一个循环中从Csv创建XML [英] Python create XML from Csv within a loop

查看:228
本文介绍了Python在一个循环中从Csv创建XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从csv



CSV创建xml文件:

  CatOne,CatTwo,CatThree 
ProdOne,ProdTwo,ProdThree
ProductOne,ProductTwo,ProductThree

所需的XML:

 < root& 
< prod>
< CatOne> ProdOne< / CatOne>
< CatTwo> ProdTwo< / CatTwo>
< CatThree> ProdThree< / CatThree>
< / prod>
< prod>
< CatOne> ProductOne< / CatOne>
< CatTwo> ProductTwo< / CatTwo>
< CatThree> ProductThree< / CatThree>
< / prod>
< / root>

这是我的代码:

 #! usr / bin / python 
# - * - 编码:utf-8 - * -
导入csv,sys,os
从lxml导入etree

def main ):
csvFile ='test.csv'
xmlFile = open('myData.xml','w')
csvData = csv.reader(open(csvFile),delimiter ='\\ \\ t')
header = csvData.next()
details = csvData.next()
details2 = csvData.next()
root = etree.Element('root' )

prod = etree.SubElement(root,'prod')
对于索引范围(0,len(header)):
child = etree.SubElement(prod, header [index])
child.text = details [index]
prod.append(child)
prod = etree.SubElement(root,'prod')
范围(0,len(header)):
child = etree.SubElement(prod,header [index])
child.text = details2 [index]
prod.append b $ b result = etree.tostring(root,pretty_print = True)
xmlFile.write(result)

如果__name__ =='__main__':
main $ b

我得到了所需的输出,但是我做的方式真的很糟糕。
我想有一些通用的方式,我相信它可能更多的pythonic
但我不知道如何做到这一点。



感谢您的帮助

如果csv有10行或甚至20行,



我会在这里回答我自己的问题,可能是帮助某人否则我希望

 #! usr / bin / python 
# - * - 编码:utf-8 - * -
导入csv,sys,os
从lxml导入etree

def main ):
csvFile ='test.csv'
xmlFile = open('myData.xml','w')
csvData = csv.reader(open(csvFile),delimiter ='\\ \\ t')

header = csvData.next()
counter = 0
root = etree.Element('root')

in csvData:
prod = etree.SubElement(root,'prod')
对于范围内的索引(0,len(header)):
child = etree.SubElement(prod,header [ index])
child.text = row [index] .decode('utf-8')
prod.append(child

result = etree.tostring(root,pretty_print = True)
xmlFile.write(result)

如果__name__ =='__main__':
main()


I am trying to create a xml file from a csv

CSV:

CatOne, CatTwo, CatThree
ProdOne, ProdTwo, ProdThree
ProductOne, ProductTwo, ProductThree

Desired XML:

<root>
  <prod>
    <CatOne>ProdOne</CatOne>
    <CatTwo>ProdTwo</CatTwo>
    <CatThree>ProdThree</CatThree>
  </prod>
  <prod>
    <CatOne>ProductOne</CatOne>
    <CatTwo>ProductTwo</CatTwo>
    <CatThree>ProductThree</CatThree>
  </prod>
</root>

Here is my code:

#! usr/bin/python
# -*- coding: utf-8 -*-
import csv, sys, os
from lxml import etree

def main():
    csvFile = 'test.csv'
    xmlFile = open('myData.xml', 'w')
    csvData = csv.reader(open(csvFile), delimiter='\t')
    header = csvData.next()
    details = csvData.next()
    details2 = csvData.next()
    root = etree.Element('root')

    prod = etree.SubElement(root,'prod')
    for index in range(0, len(header)):
        child = etree.SubElement(prod, header[index])
        child.text = details[index]
        prod.append(child)   
    prod = etree.SubElement(root,'prod')
    for index in range(0, len(header)):
        child = etree.SubElement(prod, header[index])
        child.text = details2[index]
        prod.append(child)      
    result = etree.tostring(root, pretty_print=True)
    xmlFile.write(result)  

if __name__ == '__main__':
    main()

I am getting the desired output, but the way I am doing it, is really shitty. I'd like to have it in some generic way and I believe it is possible much more pythonic But I can't figure out how to do this. The code should also work, if the csv has 10 or even 20 lines.

Thanks for help

解决方案

Ok I found out how to solve it.

I will answer my own question here, it might be help someone else I hope

#! usr/bin/python
# -*- coding: utf-8 -*-
import csv, sys, os
from lxml import etree

def main():
    csvFile = 'test.csv'
    xmlFile = open('myData.xml', 'w')
    csvData = csv.reader(open(csvFile), delimiter='\t')

    header = csvData.next()
    counter = 0
    root = etree.Element('root')

    for row in csvData:
        prod = etree.SubElement(root,'prod')
        for index in range(0, len(header)):
            child = etree.SubElement(prod, header[index])
            child.text = row[index].decode('utf-8')
            prod.append(child

    result = etree.tostring(root, pretty_print=True)
    xmlFile.write(result)

if __name__ == '__main__':
    main()

这篇关于Python在一个循环中从Csv创建XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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