Я использую фиктивные значения для показаний датчика, но это ниже должно работать:
import calendar
import numpy as np
from time import sleep
import time
while True:
# get time
timestamp = ts = calendar.timegm(time.gmtime())
# sensor readings
timestamped_sensor_readings = np.ndarray((0,), np.float32)
# get sensor placeholder values
timestamped_sensor_readings = np.append(timestamped_sensor_readings, np.random.rand(3))
timestamped_sensor_readings = np.append(timestamped_sensor_readings, np.random.rand(3))
timestamped_sensor_readings = np.append(timestamped_sensor_readings, np.random.rand(2))
# write format for readings
write_fmt = " ".join("%4.2f" for _ in timestamped_sensor_readings)
# append time
timestamped_sensor_readings = np.append(timestamped_sensor_readings, float(timestamp))
write_fmt += " %.0f"
with open("mydatafile.txt", "ab") as f:
np.savetxt(f, np.expand_dims(timestamped_sensor_readings, axis=0), fmt=write_fmt)
sleep(1)
Выход заполняется как показано ниже:
$ tail -f mydatafile.txt
0.72 0.90 0.77 0.37 0.51 0.46 0.41 0.76 1560186762
0.69 0.49 0.62 0.32 0.60 0.61 0.59 0.52 1560186763
0.17 0.42 0.97 0.07 0.83 0.67 0.48 0.43 1560186764
0.10 0.02 0.07 0.16 0.05 0.83 0.51 0.31 1560186765
0.64 0.78 0.29 0.96 0.04 0.19 0.11 0.43 1560186766
0.12 0.87 0.04 0.99 0.57 0.81 0.05 0.57 1560186767