Airidas Jeznas
Airidas Jeznas

Reputation: 19

Writing to csv file at chosen frequency

I want to write accelerometer data to a .csv file at a chosen frequency, let's say one row every 20ms. What is the best way to do this?

Here is the code I'm going to use to write to the file:

import csv

with open('innovators.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow([bandymas,"Clockwise",timestamp,accX,accY,accZ])

NOTE: I am aware that I have not declared some variables, this is just to show the method I'm using.

Upvotes: 1

Views: 313

Answers (2)

This would be the best implementation for a time critical script:

from time import sleep
from random import randint
from datetime import datetime

# open in "a" mode is the fastest option because it does not waste time reading the file.
# and we should use the "with" context only at the begining to avoid precious time delays
with open('innovators.csv', 'a') as opened_file:


    # the touple is the fastest reading object
    # so we use it to pass to the writing function:
    def append_to_file(file: type(open), row:tuple):

        # using this format:
        # [bandymas,"Clockwise",timestamp,accX,accY,accZ]
        #file.write("%i,%s,%s,%i,%i,%i;\n" % row ) # csv is not standarized yet so this format could work on some computers
        file.write("%i;%s;%s;%i;%i;%i,\n" % row ) # this is compatible with excel

    # this would be the time precious operation
    while True:

        #get the values from a sensor (in this case we are simulating them):
        now = datetime.now() # current date and time
        row = (randint(-20, 100), "Clockwise",  str(now), randint(50, 180), randint(100, 200), randint(10, 60))

        #save the row to the file
        append_to_file(opened_file, row)
        sleep(1)

Upvotes: 1

bherbruck
bherbruck

Reputation: 2226

This will append a csv file with a dictionary every 0.02 seconds (20ms):

import csv
import time


def append_csv_dict(path, data):
    '''
    Append a csv with a dictionary keys as column headers
    Args:
        path (str): Path to the csv file
        data (dict): Dictionary with keys as column headers
                     and values as column data
    '''
    with open(path, 'a') as file:
        # set the field names to the keys of the dictionary
        fieldnames = list(data.keys())
        writer = csv.DictWriter(file, fieldnames=fieldnames)
        # write the header if the file is new
        if file.tell() == 0:
            writer.writeheader()
        # write the row
        writer.writerow(data)


file_name = './test.csv'

data = {
    'first_name': 'John',
    'last_name': 'Doe'
}

interval = 0.02

# loop starts here
while True:
    append_csv_dict(file_name, data)
    time.sleep(interval)

This is going to go crazy and write a bunch of rows!

You'll have to provide your own data as a dict but it will take any dictionary.

Upvotes: 0

Related Questions