title: How to insert a new column and export it to CSV in Python?
body: <p>I have a button that exports data from MySQL into a CSV file. The fetched data may have single or multiple rows. I would like to add a new Column on the left most column called Item No(Header) for each row of data, and it's value increases based on the number of rows. If there are 3 rows, then the first row would be labelled as 1 and subsequently 2 and 3. My code performs a SQL query to fetch the data and populate it in a list. Then pass the values to another function to generate and export the CSV file. Could anyone suggest the best way to perform this? Thank you.</p>
  <p>Current Output:
  <a href="https://i.stack.imgur.com/nalIx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nalIx.png" alt="current output" /></a>
  Desired Output:
  <a href="https://i.stack.imgur.com/QnDtF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QnDtF.png" alt="desired output" /></a></p>
  <pre><code>def Company_Excel(asset_Box, ID_Box, newid_Entry, temp_Entry, gps_Entry, current_Entry, PID_Box, projectid_Entry, projectname_Entry, projectpic_Entry, piccontact_Entry, plocation_Entry, name_Entry, company_Entry, email_Entry, contact_Entry, issue_Entry, return_Entry, status_Entry, remarks_Entry, tree, company_Box):
  
      try:  
          company = company_Box.get()  
          if (company_Box.get() != &quot;Please select one&quot;):
              
              ncommand = (&quot;SELECT `ID`, `Type`, `Project`, `Name`, `Location` from `History` WHERE `Company` = %s ORDER BY `Transaction ID`&quot;)
              mycursor.execute(ncommand,(company_Box.get(),))
              nresult = mycursor.fetchall()
  
              array6 = []
              for e in range(len(nresult)):
                  nresult[e] = list(nresult[e])
                  array6.append(nresult[e])
                  
              write_to_csv_company(array6)
              
              if os.path.exists(&quot;Company.csv&quot;):
                os.remove(&quot;Company.csv&quot;)
              else:
                print(&quot;The file does not exist&quot;)
                
              tkinter.messagebox.showinfo(&quot;Exported&quot;, &quot;Success!&quot;)
  
          elif (company_Box.get() == &quot;Please select one&quot;):
              valueErrorMessage = &quot;Please select one from the drop down menu&quot;
              tkinter.messagebox.showerror(&quot;Error&quot;, valueErrorMessage)
              
          else:
              valueErrorMessage = &quot;Not found, please try again&quot;
              tkinter.messagebox.showerror(&quot;Error&quot;, valueErrorMessage)
          
      except PermissionError:
              valueErrorMessage = &quot;Please close all the excel files before exporting&quot;
              tkinter.messagebox.showerror(&quot;Error&quot;, valueErrorMessage)
              
  def write_to_csv_company(result):
  
      with open('Company.csv', 'w+', newline='') as f:
          w = csv.writer(f, dialect='excel')
          for record in result:
              w.writerow(record)
              
      with open('Company.csv',newline='') as f:
          r = csv.reader(f)
          data = [line for line in r]
      
      with open('Items by Company.csv','w',newline='') as f:
          w = csv.writer(f)
          w.writerow(['ID', 'Type', 'Project', 'Name', 'Location'])
          w.writerows(data) 
  </code></pre>