Polymatheia supports CSV both as an input and output format.
To read records from a CSV file, use the ~polymatheia.data.reader.CSVReader
, passing the filename of the CSV file to load:
python
from polymatheia.data.reader import CSVReader
reader = CSVReader('filename.csv') for record in reader: print(record)
Each row in the CSV file will become one record and each column a key within that record.
To write records to a CSV file, use the ~polymatheia.data.writer.CSVWriter
, passing the filename to write the CSV file to:
python
from polymatheia.data.writer import CSVWriter
- records = [
- {
'id': 1, 'name': 'Test Person', 'age': 32
}, { 'id': 2, 'name': 'Another Test Person', 'age': 19 }, { 'id': 3, 'name': 'Final Person', 'age': 64 }
] writer = CSVWriter('people.csv') writer.write(records)
Important
Records that are written to a CSV file must not contain any nested data, as demonstrated in the example above. Any reader can also be passed to ~polymatheia.data.writer.CSVWriter.write
, however the records provided by that reader must not contain any nested data.
You can use the transformation functionality <transforming>
to convert a nested structure into a a simple structure ready for use with ~polymatheia.data.writer.CSVWriter.write
.