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