Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python csv header ignore while keep appending data to csv file

Tags:

python

csv

I am trying to add some random data into text file and I am successful in that but I am facing problem with the header line. I want to add header line once and then every time I run my script , it should add just data into file and ignore the header line if exists. I tried something like this but I fail. I try to look at this example code in SO python csv, writing headers only once, but couldn't implement properly. If somebody help me to correct my code. I will be thankful.

import random
import csv
import os.path
from time import gmtime, strftime

filename = '/home/robdata/collection1.dat'

file_exists = os.path.isfile(filename)

v = random.randint(0, 100)

with open(filename, "a") as csvfile:
    headers = ['DATE', 'value']
    writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='\n',fieldnames=headers)
    if not file_exists:
        writer.writeheader()  # file doesn't exist yet, write a header

    writer.writerow({'DATE': strftime("%Y-%m-%d %H:%M:%S", gmtime()), 'value': v})

it insert data but without adding any header line. I want to include headers on the first run of script, and next time when I run script several times , it should only add data and not header line. thank a lot for any idea or help.

like image 499
rob Avatar asked Mar 23 '17 13:03

rob


People also ask

How to skip the header of the CSV file in Python?

All the methods in the above article are perfectly fine and are used by the Python programmer to skip the header of the CSV file while reading the CSV data. The Pandas library method not only allows us to remove the header of the CSV file data but can also be used to remove other rows if we specify their number or index position to the skiprows.

How to remove the header of a CSV file using PANDAS?

The Pandas library method not only allows us to remove the header of the CSV file data but can also be used to remove other rows if we specify their number or index position to the skiprows. So the skiprows will be able to remove all those rows whose numbers will be assigned to them.

How to append a new row into a CSV file in Python?

If you wish to append a new row into a CSV file in Python, you can use any of the following methods. Assign the desired row’s data into a List. Then, append this List’s data to the CSV file using writer.writerow(). Assign the desired row’s data into a Dictionary. Then, append this dictionary’s data to the CSV file using DictWriter.writerow().

What does append header=false mean in pandas?

header=False: Do not include a header when appending the new data. The following step-by-step example shows how to use this function in practice. Let’s create a new pandas DataFrame to append to the existing CSV file: The following code shows how to append this new data to the existing CSV file:


1 Answers

A slightly simpler alternative to Mr Evans approach would be to use the following test in place of the test for existence:

fileEmpty = os.stat('collection1.dat').st_size == 0

This obviates the need to do a seek, etc.

EDIT: Complete code:

import random
import csv
import os.path
from time import gmtime, strftime

filename = '/home/robdata/collection1.dat'

fileEmpty = os.stat(filename).st_size == 0

v = random.randint(0, 100)

with open(filename, "a") as csvfile:
    headers = ['DATE', 'value']
    writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='\n',fieldnames=headers)
    if fileEmpty:
        writer.writeheader()  # file doesn't exist yet, write a header

    writer.writerow({'DATE': strftime("%Y-%m-%d %H:%M:%S", gmtime()), 'value': v})
like image 159
Bill Bell Avatar answered Oct 17 '22 08:10

Bill Bell