118

I wrote a Python script merging two csv files, and now I want to add a header to the final csv. I tried following the suggestions reported here and I got the following error: expected string, float found. What is the most pythonic way to fix this?

Here is the code I am using:

import csv

with open('combined_file.csv', 'w', newline='') as outcsv:
    writer = csv.DictWriter(outcsv, fieldnames = ["Date", "temperature 1", "Temperature 2"])
    writer.writeheader()

    with open('t1.csv', 'r', newline='') as incsv:
        reader = csv.reader(incsv)
        writer.writerows(row + [0.0] for row in reader)

    with open('t2.csv', 'r', newline='') as incsv:
        reader = csv.reader(incsv)
        writer.writerows(row[:1] + [0.0] + row[1:] for row in reader)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
albus_c
  • 6,292
  • 14
  • 36
  • 77
  • how many columns are you writing into your csv file? Could you please specify in your question 1. input format of your file 2. output format – nio Dec 03 '13 at 10:09
  • @nio: A large section of the code posted is from [this previous question by the OP](http://stackoverflow.com/questions/20306550/python-merging-of-csv-files-with-one-axis-in-common) – Martijn Pieters Dec 03 '13 at 10:20

4 Answers4

166

The DictWriter() class expects dictionaries for each row. If all you wanted to do was write an initial header, use a regular csv.writer() and pass in a simple row for the header:

import csv

with open('combined_file.csv', 'w', newline='') as outcsv:
    writer = csv.writer(outcsv)
    writer.writerow(["Date", "temperature 1", "Temperature 2"])

    with open('t1.csv', 'r', newline='') as incsv:
        reader = csv.reader(incsv)
        writer.writerows(row + [0.0] for row in reader)

    with open('t2.csv', 'r', newline='') as incsv:
        reader = csv.reader(incsv)
        writer.writerows(row[:1] + [0.0] + row[1:] for row in reader)

The alternative would be to generate dictionaries when copying across your data:

import csv

with open('combined_file.csv', 'w', newline='') as outcsv:
    writer = csv.DictWriter(outcsv, fieldnames = ["Date", "temperature 1", "Temperature 2"])
    writer.writeheader()

    with open('t1.csv', 'r', newline='') as incsv:
        reader = csv.reader(incsv)
        writer.writerows({'Date': row[0], 'temperature 1': row[1], 'temperature 2': 0.0} for row in reader)

    with open('t2.csv', 'r', newline='') as incsv:
        reader = csv.reader(incsv)
        writer.writerows({'Date': row[0], 'temperature 1': 0.0, 'temperature 2': row[1]} for row in reader)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • This works, funny thing: when file is opened in `a` mode, `writer.writeheader()` will write down the header twice despite of the header row was written already! – loretoparisi Jul 09 '19 at 10:33
  • 3
    @loretoparisi: of course it does. Don't use `writer.writeheader()` when appending to an existing file. The `csv.writer()` object can't detect that you are writing data to an existing file. – Martijn Pieters Jul 09 '19 at 17:28
13

You just add one additional row before you execute the loop. This row contains your CSV file header name.

schema = ['a','b','c','b']
row = 4
generators = ['A','B','C','D']
with open('test.csv','wb') as csvfile:    
     writer = csv.writer(csvfile, delimiter=delimiter)
# Gives the header name row into csv
     writer.writerow([g for g in schema])   
#Data add in csv file       
     for x in xrange(rows):
         writer.writerow([g() for g in generators])
Mitul Panchal
  • 592
  • 5
  • 7
6

This worked for me.

header = ['row1', 'row2', 'row3']
some_list = [1, 2, 3]
with open('test.csv', 'wt', newline ='') as file:
    writer = csv.writer(file, delimiter=',')
    writer.writerow(i for i in header)
    for j in some_list:
        writer.writerow(j)
saggzz
  • 301
  • 3
  • 9
2

Would not this be simple that if we are creating file that time only we add header if we are not than append data to it..

  conso_file_path = 'your_folder_path\your_file.csv'
    # Create File
    if not os.path.exists(conso_file_path):
        print("No File")
        header = ['col_name 1', 'col_name 1', 'col_name 3', 'col_name4']
        with open(conso_file_path, 'w') as f:
            writer = csv.writer(f)
            writer.writerow(header)
Hietsh Kumar
  • 1,197
  • 9
  • 17