write header rows to csv python

11,211
import csv
import os

file_name = 'C:\\fa.csv'
fake_data = [[1, 2, 3, 4, 5, 4444, 6, 67], [1, 2, 3, 4, 5, 4444, 6, 67],
             [1, 2, 3, 4, 5, 4444, 6, 67], [1, 2, 3, 4, 5, 4444, 6, 67]]
headers = ['DataA', 'DataB', 'DataC', 'DataD']

if os.path.isfile(file_name):
  with open(file_name, 'a', encoding="utf-8") as outfile:
    writer = csv.writer(outfile)
    for datum in fake_data:
      writer.writerow(datum)
else:
  with open(file_name, 'w', encoding="utf-8") as outfile:
    writer = csv.writer(outfile)
    writer.writerow(headers)
    for datum in fake_data:
      writer.writerow(datum)

In the first line open('C:\\fa.csv', 'a+', newline='', encoding="utf-8") you have a+ which is append you want w which is write.

Check this out: https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

Share:
11,211

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    How do I add headers to row one in a csv?

    My solution currently appends everything

    Something like: writer.writerow(['DataA', 'DataB', 'DataC', 'DATA D'])[0]

    I feel like there an easy way of doing this and I'm overlooking the obvious.

    I've looked at a lot of examples online but am still struggling as to how you can easily do this in a csv.

    As an example lets say I wanted to scrape data of SO - load up selenium, write that into four columns and do this for 20 pages. I'd want to write the headers to row 1 each time and then append the scraped data

    with open('C:\\fa.csv', 'a+', newline='', encoding="utf-8") as outfile:
        writer = csv.writer(outfile)
        writer.writerow(['DataA', 'DataB', 'DataC', 'DataD'])
        for row in zip(ZAW_text, RESULTS1, RESULTS):
            writer.writerow(row)
            #writer.writerows({'Date': row[0], 'temperature 1': row[1], 'temperature 2': 0.0} for row in writer)
            print(row)
    
    • AtHeartEngineer
      AtHeartEngineer over 6 years
      That is pretty much how you do it. I don't think there is an easier way.
    • Admin
      Admin over 6 years
      @Tyler.Exposure Very well. Well in my case it appends the headers so it's not working for me. How do I get around that as I want headers in row one.
    • Admin
      Admin over 6 years
      Pretty sure I'm missing something. You can specify columns in csv surely rows is possible too.
    • AtHeartEngineer
      AtHeartEngineer over 6 years
      AHHH I see what you did. I'm posting an answer.
  • Admin
    Admin over 6 years
    I see. In the example with 20 other pages, will that data still be appended in the csv. Or will it all overwrite each other.
  • Admin
    Admin over 6 years
    Unfortunately. The headers need to be at row 1. The data needs to be appended as its in a loop
  • AtHeartEngineer
    AtHeartEngineer over 6 years
    Every time you run this whole function, it will overwrite the file, but as its going through the for loop it will append, because you are writing a new row each time. Make sense?
  • Admin
    Admin over 6 years
    Uh.. that makes sense. I'd rather not overwrite the file if possible and keep an ever growing list of data. Is that possible? Hmm
  • AtHeartEngineer
    AtHeartEngineer over 6 years
    So, this basically checks if the file exists, if it exists, then it will only append to it, if it doesn't exist, it will write the header row, then continue writing new rows to it.
  • AtHeartEngineer
    AtHeartEngineer over 6 years
  • Loy
    Loy about 5 years
    You can also just write a line into outfile containing the header before instantiate csv.writter object.