title: Python: Read CSV file and write to another text file
body: <p><strong>I have this .csv file ...</strong></p>
  
  <pre><code>    id,first_name,last_name,email,date,opt-in,unique_code
      1,Jimmy,Reyes,jreyes0@macromedia.com,12/29/2016,FALSE,ER45DH
      2,Doris,Wood,dwood1@1und1.de,04/22/2016,,MU34T3
      3,Steven,Miller,smiller2@go.com,07/31/2016,FALSE,G34FGH
      4,Earl,Parker,eparker3@ucoz.com,01-08-17,FALSE,ASY67J
      5,Barbara,Cruz,bcruz4@zdnet.com,12/30/2016,FALSE,NHG67P
  </code></pre>
  
  <p>If the opt-in value is empty, its should print "0". The <strong>last</strong> value in csv should print <strong>first</strong>, and then all the name, value pairs in a specific format,  like shown in the expected output file below.</p>
  
  <p><strong>My expected output</strong></p>
  
  <pre><code>ER45DH&lt;tab&gt;"id"="1","first_name"="Jimmy","last_name"="Reyes","email"="jreyes0@macromedia.com","date"="12/29/2016","opt-in"="FALSE"
  MU34T3&lt;tab&gt;"id"="2","first_name"="Doris","last_name"="Wood","email"="dwood1@1und1.de","date"="04/22/2016,"opt-in"="0"
  .......
  </code></pre>
  
  <p><strong>My code so far ..</strong></p>
  
  <pre><code>import csv
  
  with open('newfilename.csv', 'w') as f2:
      with open('mycsvfile.csv', mode='r') as infile:
          reader = csv.reader(infile)
          for i,rows in enumerate(reader):
              if i == 0:
                 header = rows 
              else:
                  if rows[5] == '':
                     rows[5] = 0;
                  pat = rows[0]+'\t'+'''"%s"="%%s",'''*(len(header)-2)+'''"%s"="%%s"‌​\n'''
                  print pat
                  f2.write(pat % tuple(header[1:]) % tuple(rows[1:]))
      f2.close()
  </code></pre>
  
  <p>This code produces this output</p>
  
  <pre><code>1   "first_name"="Jimmy","last_name"="Reyes","email"="jreyes0@macromedia.com","date"="12/29/2016","opt-in"="FALSE","unique_code"="ASD34R"‌​
  2   "first_name"="Doris","last_name"="Wood","email"="dwood1@1und1.de","date"="04/22/2016","opt-in"="0","unique_code"="SDS56N"
  </code></pre>
  
  <p><strong>As you can see column "id" is missing, and I want unque_code at first place.</strong></p>
  
  <p>I will really appreciate any help/ideas/pointers. </p>
  
  <p>Thanks</p>