Metadata-Version: 2.1
Name: list2csv
Version: 1.0.0
Summary: A simple package intended to help write iterables of objects to CSV files
Home-page: https://github.com/James-Ansley/list2csv
Author: James Finnie-Ansley
License: UNKNOWN
Platform: UNKNOWN
Classifier: Development Status :: 1 - Planning
Classifier: Topic :: Utilities
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.8
Description-Content-Type: text/markdown

# List2CSV

List2CSV is a simple package that helps with writing lists of objects to CSV files.

The main class `Writer` takes a writable file as a parameter and can have various fields added with format specifiers.
Fields can either be instance attribute names or functions that map an object to some value.
For example:

```python
from dataclasses import dataclass
from statistics import mean
from list2csv import Writer

@dataclass
class Student:
    student_id: str
    test_mark: float
    lab_marks: list[float]


students = [
    Student('abcd123', 78.5, [92.3, 98, 100, 70]),
    Student('efgh456', 62, [98, 68.2, 0, 93.5]),
    Student('ijkl789', 100, [100, 100, 98.7, 100]),
]

with open('student_overview.csv', 'w') as f:
    writer = Writer(f)
    writer.add_field('ID', 'student_id')
    writer.add_field('Test Mark', 'test_mark', '{:.2f}')
    writer.add_field('Average Lab Mark', lambda s: mean(s.lab_marks), '{:.2f}')

    writer.write_header()
    writer.write_all(students)
```

Would produce the following table:

|ID     |Test Mark|Average Lab Mark|
|-------|---------|----------------|
|abcd123|78.50    |90.08           |
|efgh456|62.00    |64.92           |
|ijkl789|100.00   |99.67           |


