DataRecorder

3.6.2 · active · verified Thu Apr 16

DataRecorder is a Python toolkit designed for efficient and reliable data recording to various file formats. It tackles common issues in data collection like frequent file I/O by caching data and writing in batches, reducing overhead and preventing data loss from unexpected program termination. It supports multithreaded writes and automatically handles file locking. The library provides specialized tools like `Recorder` for sequential data, `Filler` for filling tabular data at specific coordinates, and `ByteRecorder` for binary data. It supports `csv`, `xlsx`, `json`, `txt`, and arbitrary binary file formats. [2]

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `Recorder` class to append data to a CSV file. It shows adding individual rows and a loop for multiple entries, followed by manually calling `record()` to flush data and `close()` to ensure all buffered data is written.

from DataRecorder import Recorder
import os

# Example for Recorder
file_path = 'my_data.csv'
r = Recorder(file_path)

data_row_1 = (1, 2, 3, 4)
data_row_2 = (5, 6, 7, 8)

r.add_data(data_row_1) # Record a single row of data
r.add_data(data_row_2) # Record another single row
r.add_data('just a string') # Can also record single values

# Simulate collecting more data
for i in range(10):
    r.add_data([f'item_{i}', i * 10, True])

r.record() # Force flush any cached data to file
r.close() # Close the recorder, ensuring all data is written

print(f"Data written to {file_path}")

# Clean up the created file for re-runnability
if os.path.exists(file_path):
    os.remove(file_path)

view raw JSON →