python csv文件转换成xml, 构建新xml文件

csv文件

python csv文件转换成xml, 构建新xml文件_第1张图片

 

code

from xml.etree.ElementTree import Element,ElementTree,tostring
import json,csv

def csvtoxml(fname):
    with open(fname,'r') as f:
        reader=csv.reader(f)
        header=next(reader)
        root=Element('Daaa')
        print('root',len(root))
        for row in reader:
            erow=Element('Row')
            root.append(erow)
            for tag,text in zip(header,row):
                e=Element(tag)
                e.text=text
                erow.append(e)
    beatau(root)
    return ElementTree(root)



def beatau(e,level=0):
    if len(e)>0:
        e.text='\n'+'\t'*(level+1)
        for child in e:
           beatau(child,level+1)
        child.tail=child.tail[:-1]
    e.tail='\n' + '\t'*level
    
et=csvtoxml(r'C:\Temp\ff.csv')
et.write(r'C:\Temp\fff.xml')

 

out


                
                                summary1
                                2
                                do do
                                hi
                                ui1
                                11
                
                
                                summary2
                                3
                                do do2
                                hi2
                                ui2
                                12
                
                
                                summary2
                                4
                                do do2
                                hi2
                                ui2
                                13
                
                
                                summary4
                                5
                                do do4
                                hi
                                ui4
                                14
                
                
                                summary5
                                6
                                do do5
                                hi2
                                ui5
                                15
                
                
                                summary6
                                7
                                do do6
                                hi2
                                ui6
                                16
                
                
                                Summary
                                8
                                Status
                                Priority
                                Assignee
                                17
                
                
                                summary7
                                9
                                do do7
                                hi7
                                ui7
                                18
                
                
                                summary8
                                10
                                do do8
                                hi8
                                ui8
                                19
                

 

转载于:https://www.cnblogs.com/baxianhua/p/10478514.html

你可能感兴趣的:(python,开发工具,json)