Skip to content Skip to sidebar Skip to footer

Python Csv, Writing Headers Only Once

So I have a program that creates CSV from .Json. First I load the json file. f = open('Data.json') data = json.load(f) f.close() Then I go through it, looking for a specific keywo

Solution 1:

You could check if file is already exists and then don't call writeheader() since you're opening the file with an append option.

Something like that:

import os.path


file_exists = os.path.isfile(filename)

withopen (filename, 'a') as csvfile:
    headers = ['TimeStamp', 'light', 'Proximity']
    writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='\n',fieldnames=headers)

    ifnot file_exists:
        writer.writeheader()  # file doesn't exist yet, write a header

    writer.writerow({'TimeStamp': dic['ts'], 'light': dic['light'], 'Proximity': dic['prox']})

Solution 2:

Just another way:

withopen(file_path, 'a') as file:
        w = csv.DictWriter(file, my_dict.keys())

        if file.tell() == 0:
            w.writeheader()

        w.writerow(my_dict)

Solution 3:

You can check if the file is empty

import csv
import os

headers = ['head1', 'head2']

for row in interator:
    withopen('file.csv', 'a') as f:
        file_is_empty = os.stat('file.csv').st_size == 0
        writer = csv.writer(f, lineterminator='\n')
        if file_is_empty:
            writer.writerow(headers)
        writer.writerow(row)

Solution 4:

Can you change the structure of your code and export the whole file at once?

def write_light_csv(filename, data):
    with open (filename, 'w') as csvfile:
        headers = ['TimeStamp', 'light','Proximity']
        writer = csv.DictWriter(csvfile, delimiter=',', lineterminator='\n',fieldnames=headers)

        writer.writeheader()

        for item indata:
            if"light"in item:
                writer.writerow({'TimeStamp': item['ts'], 'light' : item['light'],'Proximity' : item['prox']})


write_light_csv('light.csv', data)

Solution 5:

I would use some flag and run a check before writing headers! e.g.

flag=0defget_data(lst):
    for i in lst:#say list of urlglobal flag
        respons = requests.get(i)
        respons= respons.content.encode('utf-8')
        respons=respons.replace('\\','')
        print respons
        data = json.loads(respons)
        fl = codecs.open(r"C:\Users\TEST\Desktop\data1.txt",'ab',encoding='utf-8')
        writer = csv.DictWriter(fl,data.keys())
        if flag==0:
            writer.writeheader()
        writer.writerow(data)
        flag+=1print"You have written % times"%(str(flag))
    fl.close()
get_data(urls)

Post a Comment for "Python Csv, Writing Headers Only Once"