Home > front end >  Data is adjacent to headers in csv
Data is adjacent to headers in csv

Time:04-18

how can I put my first row of data in the csv under the header and not in the same row as header?

This is the results. enter image description here

And down here is my coding.

import os

# ...

filename = 'C:/Desktop/GPS_Trial/Trial6/'   str(d1)   '_'   str(file_counter)  '.csv'
        
#check whether the file exist or not
rows_to_be_written = []
if not os.path.exists(filename):
    rows_to_be_written.append(header1)
    rows_to_be_written.append(header2)
    rows_to_be_written.append(header3)       
rows_to_be_written.append(gps)
rows_to_be_written.append(gps2)
rows_to_be_written.append(gps3)
                        
    #write the data into csv
with open(filename, 'a', newline='', encoding='UTF8') as f:
    writer = csv.writer(f, delimiter=',')
    writer.writerow(rows_to_be_written)
    print(gps, gps2, gps3)

CodePudding user response:

You write header with values in one row if it file not exists. You should write it separately

    rows_to_be_written = []
    header = None
    if not os.path.exists(filename):
        header = [header1, header2, header3]
    rows_to_be_written.append(gps)
    rows_to_be_written.append(gps2)
    rows_to_be_written.append(gps3)

    # write the data into csv
    with open(filename, 'a', newline='', encoding='UTF8') as f:
        writer = csv.writer(f, delimiter=',')
        if header:
            writer.writerow(header)
        writer.writerow(rows_to_be_written)
        print(gps, gps2, gps3)

Also may be you tried write rows, but you write only one row with header in it. Then change code like this

    rows_to_be_written = []
    if not os.path.exists(filename):
        rows_to_be_written.append([header1, header2, header3])
    rows_to_be_written.append([gps, gps2, gps3])

    # write the data into csv
    with open(filename, 'a', newline='', encoding='UTF8') as f:
        writer = csv.writer(f, delimiter=',')
        for row in rows_to_be_written:
            writer.writerow(row)
        print(gps, gps2, gps3)

CodePudding user response:

You need to add the headings separately, and only if they are not there already:

# check whether the file exist or not
if not os.path.exists(filename):
    headings = [header1, header2, header3]
else:
    headings = None
rows_to_be_written = [gps, gps2, gps3]

# write the data into csv
with open(filename, 'a', newline='', encoding='UTF8') as f:
    writer = csv.writer(f)
    # Write headings if exist
    if headings != None:
        writer.writerow(headings)
    # Write rows
    writer.writerow(rows_to_be_written)
    print(gps, gps2, gps3)

I suggest you consider this approach

# Open file to see if there are headings
with open(filename, "r") as f:
    try:
        has_headings = csv.Sniffer().has_header(f.read(1024))
    except csv.Error:
        # The file seems to be empty
        has_headings = False


# Open to write. In append mode ("a")
with open(filename, "a") as f:
    writer = csv.writer(f)
    if has_headings:
        # Write the rows at the top
        writer.writerow(headings_list)
    # Use writerows if youe have a 2D list, else use a for loop of writer.writerow
    writer.writerows(lists_of_rows)
  • Related