Trouble with Python order of operations/loop
<p>I have some code that is meant to convert CSV files into tab delimited files. My problem is that I cannot figure out how to write the correct values in the correct order. Here is my code:</p>

<pre><code>for file in import_dir:
    data = csv.reader(open(file))
    fields = data.next()
    new_file = export_dir+os.path.basename(file)
    tab_file = open(export_dir+os.path.basename(file), 'a+')
    for row in data:
        items = zip(fields, row)
        item = {}
        for (name, value) in items:
            item[name] = value.strip()
    tab_file.write(item['name']+'\t'+item['order_num']...)
    tab_file.write('\n'+item['amt_due']+'\t'+item['due_date']...)
</code></pre>

<p>Now, since both my <code>write</code> statements are in the <code>for row in data</code> loop, my headers are being written multiple times over. <br><br>If I outdent the first <code>write</code> statement, I'll have an obvious formatting error. <br>If I move the second <code>write</code> statement above the first and then outdent, my data will be out of order.<br><br> What can I do to make sure that the first <code>write</code> statement gets written once as a header, and the second gets written for each line in the CSV file? How do I extract the first 'write' statement outside of the loop without breaking the dictionary? Thanks!</p>
