CSV Codec¶
Codec for serialising and de-serialising sequences of resources into CSV format.
The CSV CODEC differs from many other CODECS in that data can be streamed, or read using an iterator.
CSV Codec¶
Codec for iterating a CSV file and parsing into a Resource.
The CSV codec is codec that yields multiple resources rather than a single document. The CSV codec does not support nesting of resources.
Reading data from a CSV file:
with open("my_file.csv") as f:
with resource in csv_codec.reader(f, MyResource):
...
- class odin.codecs.csv_codec.Reader(f, resource_type, full_clean=True, error_callback=None, **reader_kwargs)[source]¶
Customisable reader object.
- csv_dialect = 'excel'¶
CSV Dialect to use; defaults to the CSV libraries default value of excel.
- csv_reader()¶
CSV Reader object to use (if you wish to use unicodecsv or similar)
- default_empty_value = ''¶
The default value to use if a field is empty. This can be used to default to None.
- extra_field_names¶
Extra fields not included in header
- field_mapping¶
Index mapping of CSV fields to resource fields.
- field_names¶
Field names from resource.
- ignore_header_case = False¶
Use case-less comparison on header fields.
- includes_header = True¶
File is expected to include a header.
- strict_fields = False¶
Strictly check header fields.
- odin.codecs.csv_codec.dump(f, resources, resource_type=None, include_header=True, cls=<built-in function writer>, **kwargs)[source]¶
Dump resources into a CSV file.
- Parameters:
f – File to dump to.
resources – Collection of resources to dump.
resource_type – Resource type to use for CSV columns; if None the first resource will be used.
include_header – Write a CSV header.
cls – Writer to use when writing CSV, this should be based on
csv.writer
.kwargs – Additional parameters to be supplied to the writer instance.
- odin.codecs.csv_codec.dump_to_writer(writer, resources, resource_type=None, fields=None)[source]¶
Dump resources to a CSV writer interface.
The interface should expose the
csv.writer
interface.- Parameters:
writer (
csv.writer
) – Writer objectfields – List of fields to write
resources – Collection of resources to dump.
resource_type – Resource type to use for CSV columns; if None the first resource will be used.
- Returns:
List of fields that where written to.
- odin.codecs.csv_codec.dumps(resources, resource_type=None, cls=<built-in function writer>, **kwargs)[source]¶
Dump output to a string
- Parameters:
resources –
resources – Collection of resources to dump.
resource_type – Resource type to use for CSV columns; if None the first resource will be used.
cls – Writer to use when writing CSV, this should be based on
csv.writer
.kwargs – Additional parameters to be supplied to the writer instance.
- odin.codecs.csv_codec.reader(f, resource, includes_header=False, csv_module=<module 'csv' from '/home/docs/.asdf/installs/python/3.12.0/lib/python3.12/csv.py'>, full_clean=True, ignore_header_case=False, strict_fields=False, **kwargs)[source]¶
CSV reader that returns resource objects
- Parameters:
f – file like object
resource –
includes_header – File includes a header that should be used to map columns
csv_module – Specify an alternate csv module (eg unicodecsv); defaults to the builtin csv as this module is implemented in C.
full_clean – Perform a full clean on each object
ignore_header_case – Ignore the letter case on header
strict_fields – Extra fields cannot be provided.
- Returns:
Iterable reader object
- Return type: