首页 > 解决方案 > 以选定的频率写入 csv 文件

问题描述

我想以选定的频率将加速度计数据写入 .csv 文件,假设每 20 毫秒一行。做这个的最好方式是什么?

这是我将用来写入文件的代码:

import csv

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

注意:我知道我没有声明一些变量,这只是为了展示我正在使用的方法。

标签: pythoncsvtime

解决方案


这将是时间关键脚本的最佳实现:

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)

推荐阅读