'Add header to CSV files only once using Python - that are creating live
So I am getting live JSON data from the stock market and creating CSV files correspondingly, for each csv file we need to add header.
def data_csv(self, task):
ExchangeInstrumentName = task["Source"]
fields = ["Time", "LastRate", "Volume", "AvgRate", "High", "Low", "Source"]
csv_file = open(f"data/{ExchangeInstrumentName}.csv", "a")
csv_writer = csv.DictWriter(csv_file, delimiter=',', fieldnames=fields)
csv_writer.writerow(task)
# csv_writer.writeheader()
csv_file.close()
This is the function I am using to create the CSV files. fields is the variable in which my headers are stored. If I use writeheader() - for each row the headers are getting created.
I only want header at the top of the file. Please help
Solution 1:[1]
You could test the file size before adding a row. If it is zero bytes it will need a header. If it does not exist, the call will fail (and so will need a header).
For example:
import os
def data_csv(self, task):
ExchangeInstrumentName = task["Source"]
fields = ["Time", "LastRate", "Volume", "AvgRate", "High", "Low", "Source"]
filename = f"data/{ExchangeInstrumentName}.csv"
add_header = True
try:
# > than 0 bytes?
if os.path.getsize(filename):
add_header = False
except FileNotFoundError:
pass # does not exist
with open(filename, "a") as csv_file:
csv_writer = csv.DictWriter(csv_file, delimiter=',', fieldnames=fields)
if add_header:
csv_writer.writeheader()
csv_writer.writerow(task)
Using a with statement will ensure the file is automatically closed afterwards.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Martin Evans |
