Best way to read and write two files?
<p>Folks, looking to get the suggestions on the best way to deal with the following task: <br>
1. Read data off of a CSV file. <br>
2. Edit an XML file based on the data read in Step 1.</p>

<p>I am a Python noob. So far I am able to read the data off of a CSV file. In my Java world, I would simply pass the "read" data off to a method and iterate over and edit the XML file in that method.<br>
Can I do something similar in Python? Is there a more efficient and cleaner way of achieving the same in Python?</p>

<pre><code>import csv

ifile  = open('my-file.csv', "rb")
reader = csv.reader(ifile)

rownum = 0
for row in reader:
    #print row
    if rownum == 0:
        header = row
    else:
        colnum = 0
        name = row[1]
        desig = row[5]
        print("Name: ", name)
        print("Designation: ", desig)


    rownum += 1
    if rownum == 10:
        break

ifile.close()
</code></pre>

<p>Very similar to your solution, just uses <code>enumerate</code> and <code>with</code> instead of <code>open</code> and <code>close</code>:</p>

<pre><code>import csv

with open('my-file.csv', 'rb') as ifile:
    reader = csv.reader(ifile)
    for rownum, row in enumerate(reader):
        #print row
        if rownum == 0:
            header = row
        else:
            colnum = 0
            name = row[1]
            desig = row[5]
            print("Name: ", name)
            print("Designation: ", desig)
        if rownum == 10:
            break
</code></pre>

python xml csv scripting elementtree