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>Your question is missing a little bit of clarity (what is it that you are seeking).
Anyway, from what I understood, you are looking for an easy way to read a <strong><em>csv</em></strong> file and and print the <strong><em>ith</em></strong> columns in a certain formatting (e.g. name: ... ). 
I am assuming that your file looks like the following:</p>

<pre><code>blah,Name,blahblah,blahblahblah,blahblahblahblah,Designation
whatever,name1,whatever,whatever,whatever,Designation1
whatever,name2,whatever,whatever,whatever,Designation2
whatever,name3,whatever,whatever,whatever,Designation3
whatever,name4,whatever,whatever,whatever,Designation4
whatever,name5,whatever,whatever,whatever,Designation5
whatever,name6,whatever,whatever,whatever,Designation6
</code></pre>

<p>If that is the case, then here is what I would do. I would use the known pandas library</p>

<pre><code>import pandas as pd
</code></pre>

<p>Read the csv file into a dataframe "df"</p>

<pre><code>df = pd.read_csv('my-file.csv')
</code></pre>

<p>The variable header will hold the column names</p>

<pre><code>header = list(df) # the equivilant of your "row[0]" variable
</code></pre>

<p>Method #1 of printing the required data</p>

<pre><code>for i, j in zip(list(df['Name'].values), list(df['Designation'].values)):
    print "Name: {} \nDesignation: {}".format(i, j)
</code></pre>

<p>This prints out the following:</p>

<pre><code>Name: name1 
Designation: Designation1
Name: name2 
Designation: Designation2
Name: name3 
Designation: Designation3
Name: name4 
Designation: Designation4
Name: name5 
Designation: Designation5
Name: name6 
Designation: Designation6
</code></pre>

<p>Method #2 of printing the required data    </p>

<pre><code>df['Name'] = df['Name'].map('Name: {}'.format)
df['Designation'] = df['Designation'].map('Designation: {}'.format)
print df[['Name', 'Designation']].head(n=10)
</code></pre>

<p>Which will print out the following:</p>

<pre><code>0  Name: name1  Designation: Designation1
1  Name: name2  Designation: Designation2
2  Name: name3  Designation: Designation3
3  Name: name4  Designation: Designation4
4  Name: name5  Designation: Designation5
5  Name: name6  Designation: Designation6
</code></pre>

python xml csv scripting elementtree