CSV to XML using Python

Source: Internet
Author: User

#!/usr/bin/python#csvtoxml.py#encoding:utf-8import csv, osfrom xml.dom.minidom import  Document#prfixFile =  "Creature_data" Def createxmlfile (fileprefix):     csvfile = open (fileprefix+ '. csv ');    headline =  Csvfile.readline ()      #print  headLine    typeList =  Headline.split (', ')     doc = document ()     dataroot =  doc.createelement (fileprefix+ ' List ')     dataroot.setattribute (' Xmlns:xsi ',  " Http://www.w3.org/2001/XMLSchema-instance ")     dataroot.setattribute (' Xsi:schemalocation ',  fileprefix+ '. xsd ')     doc.appendchild (dataRoot)     csvreader  = csv.reader (CSVFile)     for line in csvReader:          #print  line        dataelt = doc.createelement (FilePrefix)          for i in range (Len (typelist)):             dataelt.setattribute (Typelist[i], line[i])          dataroot.appendchild (Dataelt)     xmlFile =  Open (fileprefix+ '. xml ', ' W ')     xmlfile.write (doc.toprettyxml (indent =  ' \ t '))     xmlfile.close () Def main ():    for root, dirs,  Files in os.walk (OS.GETCWD ()):         for fname in  files:            index =  Fname.find ('. csv ')             if index  > 0:                 #print  index, fname[: index]                 Createxmlfile (Fname[:index])                  print  "transform "  + fname +  " ok!" if __name__ ==  ' __main__ ':     main ()     input ("Game  over! ")


CSV to XML using Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.