Python code for csv to xml and xml to csv

This is very simple program to convert csv to xml and xml to csv. File needs to be passed by command line argument. program will automatically come to know which file is it by checking its extension and convert it to the other form. You can use this code for free for any purpose, you can modify the code as per your requirement. If I get time I might update csv to xml code because it wont give correct format. Although xml to csv code is working absolutely file. There are lots of ways but this is one of the novice way to use python programming. I hope you like it. Please poing mistake and you can ask doubts if you have. I know there would be lots of bugs because I am not handling any exception.

# To change this template, choose Tools | Templates
# and open the template in the editor.

__author__="shahid"
__date__ ="$Jan 10, 2012 8:42:29 PM$"
#import urllib2
from xml.dom.minidom import parseString
import sys
import re

import os
ext = os.path.splitext(sys.argv[1])[1]
classId = re.compile(' 1):
    if( ext == '.csv' ):
        counter=0
        next=0
        pre=0
        pre_teacher1=0
        pre_teacher2=0
        csvFile = sys.argv[1]
        xmlFile = "createdXmlFile.xml"
        xmlData = open(xmlFile, 'w')
        xmlData.write('' + "\n")
        xmlData.write('' + "\n")
        xmlData.write('' + "\n")
        in_file = open(csvFile, 'r')
        classes=0
        for eachLine in in_file:
            #print eachLine
            
            words = eachLine.split(',')
            

            # ("yeah its printing1")
         
            if (len(words)<2):
                print "finish"
                classes=2
                xmlData.write("\n")
                xmlData.write(' ')
                classes=0
            else:
                #print "atleast once"
                next=words[0]
                
                next_teacher1=words[2]
                next_teacher2=words[5]
            #print words[1]
                counter=0
                if(pre != next):
                    if(classes==2):
                        xmlData.write("\n")
                        xmlData.write(' ')
                        classes=0
                    xmlData.write( "\n")
                    xmlData.write( "\n")
                    xmlData.write('  ')
                    classes=2
                    pre=words[0]

                xmlData.write( "\n")
                if(words[2]!=" " and pre_teacher1 != next_teacher1):
                    pre_teacher1=words[2]
                    xmlData.write('     ')
                    xmlData.write("\n")



                if(words[5]!=" " and pre_teacher2 != next_teacher2):
                    pre_teacher2=words[5]
                    xmlData.write('     ')
                    xmlData.write("\n")
                    xmlData.write("\n")

                if(words[8] != " "):
                    xmlData.write('     ')

        #print(word[2])
        xmlData.write('' + "\n")
        xmlData.write('' + "\n")
        xmlData.close()
        print "Hey this is CSV file !! I am done :)"

    elif( ext == '.xml' ):
        
        print sys.argv[1]
        csvFile = "createdCsvFile.csv"
        csvData = open(csvFile, 'w')
        xmlFile = sys.argv[1]
        file = open(xmlFile,'r')
        data = file.read()
        file.close()

        #Class Tag
        c1=0
        c2=0
        c3=0
        classDm = parseString(data)
        classcounter=len(classDm.getElementsByTagName('classroom'))
        for i in range(0,classcounter):
            cXmlTag = classDm.getElementsByTagName('classroom')[i].toxml()
            cXmlData=cXmlTag.replace('','')
            #print cXmlTag
            

            #Teacher Tag
            #print"Teacher"
            teacherDm = parseString(cXmlTag)
            teacherCounter = len(teacherDm.getElementsByTagName('teacher'))
            for j in range (0,teacherCounter):
                tXmlTag = teacherDm.getElementsByTagName('teacher')[j].toxml()
                tXmlData=tXmlTag.replace('','')
                #print tXmlData
                


            
            #csvData.write("\n ")

            #Student Tag
            studentDm = parseString(cXmlTag)
            studentCounter = len(studentDm.getElementsByTagName('student'))
            #print "Student:"
            for k in range (0,studentCounter):
                sXmlTag = studentDm.getElementsByTagName('student')[k].toxml()
                sXmlData=sXmlTag.replace('','')
                teacherLName=lastName.findall(tXmlData)
                teacherFName=firstName.findall(tXmlData)
                teacherId=id.findall(tXmlData)
                classIds=classId.findall(cXmlTag)
                c1=0
                c2=0
                c3=0
            #print classIds
                for p in (classIds.pop(0) for _ in xrange(len(classIds))):
                    csvData.write(p)
                    csvData.write(", ")
                classNames=className.findall(cXmlTag)
            #print classNames
                for p in (classNames.pop(0) for _ in xrange(len(classNames))):
                    csvData.write(p)
                    csvData.write(", ")


                
                for s in (teacherId.pop(0) for _ in xrange(len(teacherId))):
                    print s
                    csvData.write(s)
                    csvData.write(", ")
                    c1=c1+1
                    



                    for q in (teacherFName.pop(0) for _ in xrange(len(teacherFName))):
                        c2=c2+1
                        #print q
                        if(c1==c2):
                            #print c1,c2
                            #print q
                           
                            csvData.write(q)
                            csvData.write(", ")
                            #nameCounter = nameCounter + 1

                
                        for r in (teacherLName.pop(0) for _ in xrange(len(teacherLName))):
                            c3=c3+1
                            if(c1==c3):
                                #print c1,c3
                                csvData.write(r)
                                csvData.write(", ")
                if(c1<2):
                    csvData.write(", , ,")

                studentId=id.findall(sXmlTag)
                for p in (studentId.pop(0) for _ in xrange(len(studentId))):
                    csvData.write(p)
                    csvData.write(", ")

                studentFName=firstName.findall(sXmlData)
                for p in (studentFName.pop(0) for _ in xrange(len(studentFName))):
                    csvData.write(p)
                    csvData.write(", ")
                    nameCounter = nameCounter + 1

                studentLName=lastName.findall(sXmlData)
                for p in (studentLName.pop(0) for _ in xrange(len(studentLName))):
                    csvData.write(p)
                    #print p
                    csvData.write(", ")
                    csvData.write("\n")
                
                #print sXmlData
                
          
    else:
        print "Please enter either CSV or xml file"
else:
    print "Please enter the file extension"

To run the program please copy and paste the code in a file, open command prompt. Please make sure python is installed. In command prompt type "python .py
.py will be the name of the file where you save the program and will be either .xml or .csv file. it will create the file in the same directory where the source code is.