Files#
Utilities for CSV file handling and data transformations.
Quick Examples#
Reading CSV Files#
from core_mixins.files import records_from_csv
# Read CSV file
for record in records_from_csv("data.csv"):
print(record) # Each record is a dictionary
Writing CSV Files#
from core_mixins.files import records_to_csv
records = [
{"name": "Alice", "age": 30, "city": "NYC"},
{"name": "Bob", "age": 25, "city": "LA"},
]
# Write to CSV file
records_to_csv(
file_path="output.csv",
records=records,
columns=["name", "age", "city"],
with_headers=True
)
In-Memory CSV Operations#
from core_mixins.files import records_to_buffer, records_from_buffer
records = [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
]
# Create CSV in memory
buffer = records_to_buffer(records, columns=["name", "age"])
# Read from buffer
csv_string = buffer.getvalue()
for record in records_from_buffer(csv_string):
print(record)
API Reference#
This module provides a set of functions that could be useful in files and data manipulation.
- core_mixins.files.utils.records_from_csv(path: str, file_args: Dict | None = None, reader_args: Dict | None = None, encoding: str | None = 'utf-8') Iterator[Dict][source]#
Returns records from a CSV file…
- Returns:
An iterable of dictionaries.
- Return type:
Iterator[Dict]
- core_mixins.files.utils.records_from_buffer(data: str, newline: str | None = None, **kwargs) Iterator[Dict][source]#
Returns records from file-like object…
- Returns:
An iterable of dictionaries.
- Return type:
Iterator[Dict]
- core_mixins.files.utils.records_to_buffer(records: List[Dict], columns: List[str] | None = None, with_headers: bool = True, **kwargs) StringIO[source]#
Save records from file-like object…
- core_mixins.files.utils.records_to_csv(file_path: str, records: ~typing.Iterable[~typing.Dict], columns: ~typing.List[str], with_headers: bool = True, quoting: ~typing.Literal[0, 1, 2, 3] = 1, encoding: str | None = 'utf-8', dialect=<class 'csv.excel'>, file_args: ~typing.Dict | None = None, writer_args: ~typing.Dict | None = None) str[source]#
It writes records (dictionaries) to a CSV file…
- Parameters:
file_path (str) – The path where the CSV file will be saved.
records (Iterable[Dict]) – A collection of dictionary records to write.
columns (List[str]) – A list of column names specifying the order of fields.
with_headers (bool, optional) – Whether to include column headers in the CSV (default is True).
quoting (int, optional) – Quoting style for the CSV (default is csv.QUOTE_ALL).
encoding (str) – The encoding to use.
dialect (csv.Dialect, optional) – The CSV dialect to use (default is csv.excel).
file_args (Dict, optional) – Additional arguments for open() when writing the file (default is None).
writer_args (Dict, optional) – Additional arguments for csv.DictWriter (default is None).
- Returns:
The file path of the saved CSV.
- Return type:
- Raises:
ValueError – If columns is empty or records contain keys not in columns.
IOError – If there is an error writing to the file.