[![Build Status](https://travis-ci.org/dfurtado/dataclass-csv.svg?branch=master)](https://travis-ci.org/dfurtado/dataclass-csv)
[![pypi](https://img.shields.io/pypi/v/dataclass-csv.svg)](https://pypi.python.org/pypi/dataclass-csv)
[![Downloads](https://pepy.tech/badge/dataclass-csv)](https://pepy.tech/project/dataclass-csv)
# Dataclass CSV
Dataclass CSV makes working with CSV files easier and much better than working with Dicts. It uses Python's Dataclasses to store data of every row on the CSV file and also uses type annotations which enables proper type checking and validation.
## Main features
- Use `dataclasses` instead of dictionaries to represent the rows in the CSV file.
- Take advantage of the `dataclass` properties type annotation. `DataclassReader` use the type annotation to perform validation of the data of the CSV file.
- Automatic type conversion. `DataclassReader` supports `str`, `int`, `float`, `complex`, `datetime` and `bool`, as well as any type whose constructor accepts a string as its single argument.
- Helps you troubleshoot issues with the data in the CSV file. `DataclassReader` will show exactly in which line of the CSV file contain errors.
- Extract only the data you need. It will only parse the properties defined in the `dataclass`
- Familiar syntax. The `DataclassReader` is used almost the same way as the `DictReader` in the standard library.
- It uses `dataclass` features that let you define metadata properties so the data can be parsed exactly the way you want.
- Make the code cleaner. No more extra loops to convert data to the correct type, perform validation, set default values, the `DataclassReader` will do all this for you.
- In additon of the `DataclassReader` the library also provides a `DataclassWriter` which enables creating a CSV file
using a list of instances of a dataclass.
## Installation
```shell
pipenv install dataclass-csv
```
## Getting started
## Using the DataclassReader
First, add the necessary imports:
```python
from dataclasses import dataclass
from dataclass_csv import DataclassReader
```
Assuming that we have a CSV file with the contents below:
```text
firstname,email,age
Elsa,elsa@test.com, 11
Astor,astor@test.com, 7
Edit,edit@test.com, 3
Ella,ella@test.com, 2
```
Let's create a dataclass that will represent a row in the CSV file above:
```python
@dataclass
class User:
firstname: str
email: str
age: int
```
The dataclass `User` has 3 properties, `firstname` and `email` is of type `str` and `age` is of type `int`.
To load and read the contents of the CSV file we do the same thing as if we would be using the `DictReader` from the `csv` module in the Python's standard library. After opening the file we create an instance of the `DataclassReader` passing two arguments. The first is the `file` and the second is the dataclass that we wish to use to represent the data of every row of the CSV file. Like so:
```python
with open(filename) as users_csv:
reader = DataclassReader(users_csv, User)
for row in reader:
print(row)
```
The `DataclassReader` internally uses the `DictReader` from the `csv` module to read the CSV file which means that you can pass the same arguments that you would pass to the `DictReader`. The complete argument list is shown below:
```python
dataclass_csv.DataclassReader(
f,
cls,
fieldnames=None,
restkey=None,
restval=None,
dialect='excel',
*args,
**kwds
)
```
All keyword arguments support by `DictReader` are supported by the `DataclassReader`, with the addition of:
`validate_header` - The `DataclassReader` will raise a `ValueError` if the CSV file cointain columns with the same name. This
validation is performed to avoid data being overwritten. To skip this validation set `validate_header=False` when creating a
instance of the `DataclassReader`, see an example below:
```python
reader = DataclassReader(f, User, validate_header=False)
```
If you run this code you should see an output like this:
```python
User(firstname='Elsa', email='elsa@test.com', age=11)
User(firstname='Astor', email='astor@test.com', age=7)
User(firstname='Edit', email='edit@test.com', age=3)
User(firstname='Ella', email='ella@test.com', age=2)
```
### Error handling
One of the advantages of using the `DataclassReader` is that it makes it easy to detect when the type of data in the CSV file is not what your application's model is expecting. And, the `DataclassReader` shows errors that will help to identify the rows with problem in your CSV file.
For example, say we change the contents of the CSV file shown in the **Getting started** section and, modify the `age` of the user Astor, let's change it to a string value:
```text
Astor, astor@test.com, test
```
Remember that in the dataclass `User` the `age` property is annotated with `int`. If we run the code again an exception will be raised with the message below:
```text
dataclass_csv.exceptions.CsvValueError: The field `age` is defined as <class 'int'> but
received a value of type <class 'str'>. [CSV Line number: 3]
```
Note that apart from telling what the error was, the `DataclassReader` will also show which line of the CSV file contain the data with errors.
### Default values
The `DataclassReader` also handles properties with default values. Let's modify the dataclass `User` and add a default value for the field `email`:
```python
from dataclasses import dataclass
@dataclass
class User:
firstname: str
email: str = 'Not specified'
age: int
```
And we modify the CSV file and remove the email for the user Astor:
```python
Astor,, 7
```
If we run the code we should see the output below:
```text
User(firstname='Elsa', email='elsa@test.com', age=11)
User(firstname='Astor', email='Not specified', age=7)
User(firstname='Edit', email='edit@test.com', age=3)
User(firstname='Ella', email='ella@test.com', age=2)
```
Note that now the object for the user Astor have the default value `Not specified` assigned to the email property.
Default values can also be set using `dataclasses.field` like so:
```python
from dataclasses import dataclass, field
@dataclass
class User:
firstname: str
email: str = field(default='Not specified')
age: int
```
### Mapping dataclass fields to columns
The mapping between a dataclass property and a column in the CSV file will be done automatically if the names match, however, there are situations that the name of the header for a column is different. We can easily tell the `DataclassReader` how the mapping should be done using the method `map`. Assuming that we have a CSV file with the contents below:
```text
First Name,email,age
Elsa,elsa@test.com, 11
```
Note that now, the column is called **First Name** and not **firstname**
And we can use the method `map`, like so:
```python
reader = DataclassReader(users_csv, User)
reader.map('First name').to('firstname')
```
Now the DataclassReader will know how to extract the data from the column **First Name** and add it to the to dataclass property **firstname**
### Supported type annotation
At the moment the `DataclassReader` support `int`, `str`, `float`, `complex`, `datetime`, and `bool`. When defining a `datetime` property, it is necessary to use the `dateformat` decorator, for example:
```python
from dataclasses import dataclass
from datetime import datetime
from dataclass_csv import DataclassReader, dateformat
@dataclass
@dateformat('%Y/%m/%d')
class User:
name: str
email: str
birthday: datetime
if __name__ == '__main__':
with open('users.csv') as f:
reader = DataclassReader(f, User)
for row in reader:
print(row)
```
Assuming that the CSV file have the following contents:
```text
name,email,birthday
Edit,edit@test.com,2018/11/23
```
The output would look like this:
```text
User(name='Edit', email='edit@test.com', birthday=datetime.datetime(2018, 11, 23, 0, 0))
```
### Fields metadata
It is important to note that the `dateformat` decorator will define the date format that will be used to parse date to all properties
in the class. Now there are situations where the data in a CSV file contains two or more columns with date values in different formats. It is possible
to set a format specific for every property using the `dataclasses.field`. Let's say that we now have a CSV file with the following contents:
```text
name,email,birthday, create_date
Edit,edit@test.com,2018/11/23,2018/11/23 10:43
```
As you can see the `create_date` contains time information as well.
The `dataclass` User can be defined like this:
```python
from dataclasses import dataclass, field
from datetime import datetime
from dataclass_csv import DataclassReader, dateformat
@dataclass
@dateformat('%Y/%m/%d')
class User:
name: str
email: str
birthday: datetime
create_date: datetime = field(metadata={'dateformat': '%Y/%m/%d %H:%M'})
```
Note that the format for the `birthday` field was not speficied using the `field` metadata. In this case the format specified in the `dateformat`
decorator will be used.
### Handling values with empty spaces
When defining a property of type `str` in the `dataclass`, the `DataclassReader` will treat values with only white spaces as invalid. To change this
behavior, there is a decorator called `@accept_whitespaces`. When decorating the class with the `@accept_whitespaces` all the properties in the class
will accept values with only white spaces.
For example:
```python
from dataclass_csv import DataclassReader, accept_whitespaces
@accept_whitespaces
@dataclass
class User:
name: str
email: str
birthday: datetime
created_at: datetime
```
If you need a specific field to accept white spaces, you can set the property `accept_whitespaces` in the field's metadata, like so:
```python
@dataclass
class User:
name: str
email: str = field(metadata={'accept_whitespaces': True})
birthday: datetime
created_at: datetime
```
### User-defined types
You can use any type for a field as long as its constructor accepts a string:
```python
class SSN:
def __init__(self, val):
if re.match(r"\d{9}", val):
self.val = f"{val[0:3]}-{val[3:5]}-{val[5:9]}"
elif re.match(r"\d{3}-\d{2}-\d{4}", val):
self.val = val
else:
raise ValueError(f"Invalid SSN: {val!r}")
@dataclasses.dataclass
class User:
name: str
ssn: SSN
```
## Using the DataclassWriter
Reading a CSV file using the `DataclassReader` is great and gives us the type-safety of Python's dataclasses and type annotation, however, there are situations where we would like to use dataclasses for creating CSV files, that's where the `DataclassWriter` comes in handy.
Using the `DataclassWriter` is quite simple. Given that we have a dataclass `User`:
```python
from dataclasses import dataclass
@dataclass
class User:
firstname: str
lastname: str
age: int
```
And in your program we have a list of users:
```python
users = [
User(firstname="John", lastname="Smith", age=40),
User(firstname="Daniel", lastname="Nilsson", age=10),
User(firstname="Ella", "Fralla", age=4)
]
```
In order to create a CSV using the `DataclassWriter` import it from `dataclass_csv`:
```python
from dataclass_csv import DataclassWriter
```
Initialize it with the required arguments and call the method `write`:
```python
with open("users.csv", "w") as f:
w = DataclassWriter(f, users, User)
w.write()
```
That's it! Let's break down the snippet above.
First, we open a file called `user.csv` for writing. After that, an instance of the `DataclassWriter` is created. To create a `DataclassWriter` we need to pass the `file`, the list of `User` instances, and lastly, the type, which in this case is `User`.
The type is required since the writer uses it when trying to figure out the CSV header. By default, it will use the names of the
properties defined in the dataclass, in the case of the dataclass `User` the title of each column
will be `firstname`, `lastname` and `age`.
See below the CSV created out of a list of `User`:
```text
firstname,lastname,age
John,Smith,40
Daniel,Nilsson,10
Ella,Fralla,4
```
The `DataclassWriter` also takes a `**fmtparams` which accepts the same parameters as the `csv.writer`, for more
information see: https://docs.python.org/3/library/csv.html#csv-fmt-params
Now, there are situations where we don't want to write the CSV header. In this case, the method `write` of
the `DataclassWriter` accepts an extra argument, called `skip_header`. The default value is `False` and when set to
`True` it will skip the header.
#### Modifying the CSV header
As previously mentioned the `DataclassWriter` uses the names of the properties defined in the dataclass as the CSV header titles, however,
depending on your use case it makes sense to change it. The `DataclassWriter` has a `map` method just for this purpose.
Using the `User` dataclass with the properties `firstname`, `lastname` and `age`. The snippet below shows how to change `firstname` to `First name` and `lastname` to `Last name`:
```python
with open("users.csv", "w") as f:
w = DataclassWriter(f, users, User)
# Add mappings for firstname and lastname
w.map("firstname").to("First name")
w.map("lastname").to("Last name")
w.write()
```
The CSV output of the snippet above will be:
```text
First name,Last name,age
John,Smith,40
Daniel,Nilsson,10
Ella,Fralla,4
```
## Copyright and License
Copyright (c) 2018 Daniel Furtado. Code released under BSD 3-clause license
## Credits
This package was created with [Cookiecutter](https://github.com/audreyr/cookiecutter) and the [audreyr/cookiecutter-pypackage](https://github.com/audreyr/cookiecutter-pypackage) project template.
# History
### 0.1.0 (2018-11-25)
* First release on PyPI.
### 0.1.1 (2018-11-25)
* Documentation fixes.
### 0.1.2 (2018-11-25)
* Documentation fixes.
### 0.1.3 (2018-11-26)
* Bug fixes
* Removed the requirement of setting the dataclass init to `True`
### 0.1.5 (2018-11-29)
* Support for parsing datetime values.
* Better handling when default values are set to `None`
### 0.1.6 (2018-12-01)
* Added support for reader default values from the default property of the `dataclasses.field`.
* Added support for allowing string values with only white spaces in a class level using the `@accept_whitespaces` decorator or through the `dataclasses.field` metadata.
* Added support for specifying date format using the `dataclasses.field` metadata.
### 0.1.7 (2018-12-01)
* Added support for default values from `default_factory` in the field's metadata. This allows adding mutable default values to the dataclass properties.
### 1.0.0 (2018-12-16)
* When a data does not pass validation it shows the line number in the CSV file where the data contain errors.
* Improved error handling.
* Changed the usage of the `@accept_whitespaces` decorator.
* Updated documentation.
### 1.0.1 (2019-01-29)
* Fixed issue when parsing headers on a CSV file with trailing white spaces.
### 1.1.0 (2019-02-17)
* Added support for boolean values.
* Docstrings
### 1.1.1 (2019-02-17)
* Documentation fixes.
### 1.1.2 (2019-02-17)
* Documentation fixes.
### 1.1.3 (2020-03-01)
* Handle properties with init set to False
* Handle Option type annotation
### 1.2.0 (2021-03-02)
* Introduction of a DataclassWriter
* Added type hinting to external API
* Documentation updates
* Bug fixes
## 1.3.0 (2021-04-10)
* Included stub files
* check if the CSV file has duplicated header values
* Fixed issues #22 and #33
* code cleanup
## 1.4.0 (2021-12-13)
* Bug fixes
* Support for date types
Raw data
{
"_id": null,
"home_page": "https://github.com/dfurtado/dataclass-csv",
"name": "dataclass-csv",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "dataclass dataclasses csv dataclass-csv",
"author": "Daniel Furtado",
"author_email": "daniel@dfurtado.com",
"download_url": "https://files.pythonhosted.org/packages/ca/78/caaf00f74a195f84181e88084706f3bb1ea94d7ae428bbb0f47630318a9e/dataclass-csv-1.4.0.tar.gz",
"platform": "",
"description": "[![Build Status](https://travis-ci.org/dfurtado/dataclass-csv.svg?branch=master)](https://travis-ci.org/dfurtado/dataclass-csv)\n[![pypi](https://img.shields.io/pypi/v/dataclass-csv.svg)](https://pypi.python.org/pypi/dataclass-csv)\n[![Downloads](https://pepy.tech/badge/dataclass-csv)](https://pepy.tech/project/dataclass-csv)\n\n\n\n# Dataclass CSV\n\nDataclass CSV makes working with CSV files easier and much better than working with Dicts. It uses Python's Dataclasses to store data of every row on the CSV file and also uses type annotations which enables proper type checking and validation.\n\n\n## Main features\n\n- Use `dataclasses` instead of dictionaries to represent the rows in the CSV file.\n- Take advantage of the `dataclass` properties type annotation. `DataclassReader` use the type annotation to perform validation of the data of the CSV file.\n- Automatic type conversion. `DataclassReader` supports `str`, `int`, `float`, `complex`, `datetime` and `bool`, as well as any type whose constructor accepts a string as its single argument.\n- Helps you troubleshoot issues with the data in the CSV file. `DataclassReader` will show exactly in which line of the CSV file contain errors.\n- Extract only the data you need. It will only parse the properties defined in the `dataclass`\n- Familiar syntax. The `DataclassReader` is used almost the same way as the `DictReader` in the standard library.\n- It uses `dataclass` features that let you define metadata properties so the data can be parsed exactly the way you want.\n- Make the code cleaner. No more extra loops to convert data to the correct type, perform validation, set default values, the `DataclassReader` will do all this for you.\n- In additon of the `DataclassReader` the library also provides a `DataclassWriter` which enables creating a CSV file\nusing a list of instances of a dataclass.\n\n\n## Installation\n\n```shell\npipenv install dataclass-csv\n```\n\n## Getting started\n\n## Using the DataclassReader\n\nFirst, add the necessary imports:\n\n```python\nfrom dataclasses import dataclass\n\nfrom dataclass_csv import DataclassReader\n```\n\nAssuming that we have a CSV file with the contents below:\n```text\nfirstname,email,age\nElsa,elsa@test.com, 11\nAstor,astor@test.com, 7\nEdit,edit@test.com, 3\nElla,ella@test.com, 2\n```\n\nLet's create a dataclass that will represent a row in the CSV file above:\n```python\n@dataclass\nclass User:\n firstname: str\n email: str\n age: int\n```\n\nThe dataclass `User` has 3 properties, `firstname` and `email` is of type `str` and `age` is of type `int`.\n\nTo load and read the contents of the CSV file we do the same thing as if we would be using the `DictReader` from the `csv` module in the Python's standard library. After opening the file we create an instance of the `DataclassReader` passing two arguments. The first is the `file` and the second is the dataclass that we wish to use to represent the data of every row of the CSV file. Like so:\n\n```python\nwith open(filename) as users_csv:\n reader = DataclassReader(users_csv, User)\n for row in reader:\n print(row)\n```\n\nThe `DataclassReader` internally uses the `DictReader` from the `csv` module to read the CSV file which means that you can pass the same arguments that you would pass to the `DictReader`. The complete argument list is shown below:\n\n```python\ndataclass_csv.DataclassReader(\n f,\n cls,\n fieldnames=None,\n restkey=None,\n restval=None,\n dialect='excel',\n *args,\n **kwds\n)\n```\n\nAll keyword arguments support by `DictReader` are supported by the `DataclassReader`, with the addition of:\n\n`validate_header` - The `DataclassReader` will raise a `ValueError` if the CSV file cointain columns with the same name. This\nvalidation is performed to avoid data being overwritten. To skip this validation set `validate_header=False` when creating a\ninstance of the `DataclassReader`, see an example below:\n\n```python\nreader = DataclassReader(f, User, validate_header=False)\n```\n\nIf you run this code you should see an output like this:\n\n```python\nUser(firstname='Elsa', email='elsa@test.com', age=11)\nUser(firstname='Astor', email='astor@test.com', age=7)\nUser(firstname='Edit', email='edit@test.com', age=3)\nUser(firstname='Ella', email='ella@test.com', age=2)\n```\n\n### Error handling\n\nOne of the advantages of using the `DataclassReader` is that it makes it easy to detect when the type of data in the CSV file is not what your application's model is expecting. And, the `DataclassReader` shows errors that will help to identify the rows with problem in your CSV file.\n\nFor example, say we change the contents of the CSV file shown in the **Getting started** section and, modify the `age` of the user Astor, let's change it to a string value:\n\n```text\nAstor, astor@test.com, test\n```\n\nRemember that in the dataclass `User` the `age` property is annotated with `int`. If we run the code again an exception will be raised with the message below:\n\n```text\ndataclass_csv.exceptions.CsvValueError: The field `age` is defined as <class 'int'> but\nreceived a value of type <class 'str'>. [CSV Line number: 3]\n```\n\nNote that apart from telling what the error was, the `DataclassReader` will also show which line of the CSV file contain the data with errors.\n\n### Default values\n\nThe `DataclassReader` also handles properties with default values. Let's modify the dataclass `User` and add a default value for the field `email`:\n\n```python\nfrom dataclasses import dataclass\n\n\n@dataclass\nclass User:\n firstname: str\n email: str = 'Not specified'\n age: int\n```\n\nAnd we modify the CSV file and remove the email for the user Astor:\n\n```python\nAstor,, 7\n```\n\nIf we run the code we should see the output below:\n\n```text\nUser(firstname='Elsa', email='elsa@test.com', age=11)\nUser(firstname='Astor', email='Not specified', age=7)\nUser(firstname='Edit', email='edit@test.com', age=3)\nUser(firstname='Ella', email='ella@test.com', age=2)\n```\n\nNote that now the object for the user Astor have the default value `Not specified` assigned to the email property.\n\nDefault values can also be set using `dataclasses.field` like so:\n\n```python\nfrom dataclasses import dataclass, field\n\n\n@dataclass\nclass User:\n firstname: str\n email: str = field(default='Not specified')\n age: int\n```\n\n### Mapping dataclass fields to columns\n\nThe mapping between a dataclass property and a column in the CSV file will be done automatically if the names match, however, there are situations that the name of the header for a column is different. We can easily tell the `DataclassReader` how the mapping should be done using the method `map`. Assuming that we have a CSV file with the contents below:\n\n```text\nFirst Name,email,age\nElsa,elsa@test.com, 11\n```\n\nNote that now, the column is called **First Name** and not **firstname**\n\nAnd we can use the method `map`, like so:\n\n```python\nreader = DataclassReader(users_csv, User)\nreader.map('First name').to('firstname')\n```\n\nNow the DataclassReader will know how to extract the data from the column **First Name** and add it to the to dataclass property **firstname**\n\n### Supported type annotation\n\nAt the moment the `DataclassReader` support `int`, `str`, `float`, `complex`, `datetime`, and `bool`. When defining a `datetime` property, it is necessary to use the `dateformat` decorator, for example:\n\n```python\nfrom dataclasses import dataclass\nfrom datetime import datetime\n\nfrom dataclass_csv import DataclassReader, dateformat\n\n\n@dataclass\n@dateformat('%Y/%m/%d')\nclass User:\n name: str\n email: str\n birthday: datetime\n\n\nif __name__ == '__main__':\n\n with open('users.csv') as f:\n reader = DataclassReader(f, User)\n for row in reader:\n print(row)\n```\n\nAssuming that the CSV file have the following contents:\n\n```text\nname,email,birthday\nEdit,edit@test.com,2018/11/23\n```\n\nThe output would look like this:\n\n```text\nUser(name='Edit', email='edit@test.com', birthday=datetime.datetime(2018, 11, 23, 0, 0))\n```\n\n### Fields metadata\n\nIt is important to note that the `dateformat` decorator will define the date format that will be used to parse date to all properties\nin the class. Now there are situations where the data in a CSV file contains two or more columns with date values in different formats. It is possible\nto set a format specific for every property using the `dataclasses.field`. Let's say that we now have a CSV file with the following contents:\n\n```text\nname,email,birthday, create_date\nEdit,edit@test.com,2018/11/23,2018/11/23 10:43\n```\n\nAs you can see the `create_date` contains time information as well.\n\nThe `dataclass` User can be defined like this:\n\n```python\nfrom dataclasses import dataclass, field\nfrom datetime import datetime\n\nfrom dataclass_csv import DataclassReader, dateformat\n\n\n@dataclass\n@dateformat('%Y/%m/%d')\nclass User:\n name: str\n email: str\n birthday: datetime\n create_date: datetime = field(metadata={'dateformat': '%Y/%m/%d %H:%M'})\n```\n\nNote that the format for the `birthday` field was not speficied using the `field` metadata. In this case the format specified in the `dateformat`\ndecorator will be used.\n\n### Handling values with empty spaces\n\nWhen defining a property of type `str` in the `dataclass`, the `DataclassReader` will treat values with only white spaces as invalid. To change this\nbehavior, there is a decorator called `@accept_whitespaces`. When decorating the class with the `@accept_whitespaces` all the properties in the class\nwill accept values with only white spaces.\n\nFor example:\n\n```python\nfrom dataclass_csv import DataclassReader, accept_whitespaces\n\n@accept_whitespaces\n@dataclass\nclass User:\n name: str\n email: str\n birthday: datetime\n created_at: datetime\n```\n\nIf you need a specific field to accept white spaces, you can set the property `accept_whitespaces` in the field's metadata, like so:\n\n```python\n@dataclass\nclass User:\n name: str\n email: str = field(metadata={'accept_whitespaces': True})\n birthday: datetime\n created_at: datetime\n```\n\n### User-defined types\n\nYou can use any type for a field as long as its constructor accepts a string:\n\n```python\nclass SSN:\n def __init__(self, val):\n if re.match(r\"\\d{9}\", val):\n self.val = f\"{val[0:3]}-{val[3:5]}-{val[5:9]}\"\n elif re.match(r\"\\d{3}-\\d{2}-\\d{4}\", val):\n self.val = val\n else:\n raise ValueError(f\"Invalid SSN: {val!r}\")\n\n\n@dataclasses.dataclass\nclass User:\n name: str\n ssn: SSN\n```\n\n\n## Using the DataclassWriter\n\nReading a CSV file using the `DataclassReader` is great and gives us the type-safety of Python's dataclasses and type annotation, however, there are situations where we would like to use dataclasses for creating CSV files, that's where the `DataclassWriter` comes in handy.\n\nUsing the `DataclassWriter` is quite simple. Given that we have a dataclass `User`:\n\n```python\nfrom dataclasses import dataclass\n\n\n@dataclass\nclass User:\n firstname: str\n lastname: str\n age: int\n```\n\nAnd in your program we have a list of users:\n\n```python\n\nusers = [\n User(firstname=\"John\", lastname=\"Smith\", age=40),\n User(firstname=\"Daniel\", lastname=\"Nilsson\", age=10),\n User(firstname=\"Ella\", \"Fralla\", age=4)\n]\n```\n\nIn order to create a CSV using the `DataclassWriter` import it from `dataclass_csv`:\n\n```python\nfrom dataclass_csv import DataclassWriter\n```\n\nInitialize it with the required arguments and call the method `write`:\n\n```python\nwith open(\"users.csv\", \"w\") as f:\n w = DataclassWriter(f, users, User)\n w.write()\n```\n\nThat's it! Let's break down the snippet above.\n\nFirst, we open a file called `user.csv` for writing. After that, an instance of the `DataclassWriter` is created. To create a `DataclassWriter` we need to pass the `file`, the list of `User` instances, and lastly, the type, which in this case is `User`.\n\nThe type is required since the writer uses it when trying to figure out the CSV header. By default, it will use the names of the\nproperties defined in the dataclass, in the case of the dataclass `User` the title of each column\nwill be `firstname`, `lastname` and `age`.\n\nSee below the CSV created out of a list of `User`:\n\n```text\nfirstname,lastname,age\nJohn,Smith,40\nDaniel,Nilsson,10\nElla,Fralla,4\n```\n\nThe `DataclassWriter` also takes a `**fmtparams` which accepts the same parameters as the `csv.writer`, for more\ninformation see: https://docs.python.org/3/library/csv.html#csv-fmt-params\n\nNow, there are situations where we don't want to write the CSV header. In this case, the method `write` of\nthe `DataclassWriter` accepts an extra argument, called `skip_header`. The default value is `False` and when set to\n`True` it will skip the header.\n\n#### Modifying the CSV header\n\nAs previously mentioned the `DataclassWriter` uses the names of the properties defined in the dataclass as the CSV header titles, however,\ndepending on your use case it makes sense to change it. The `DataclassWriter` has a `map` method just for this purpose.\n\n Using the `User` dataclass with the properties `firstname`, `lastname` and `age`. The snippet below shows how to change `firstname` to `First name` and `lastname` to `Last name`:\n\n ```python\n with open(\"users.csv\", \"w\") as f:\n w = DataclassWriter(f, users, User)\n\n # Add mappings for firstname and lastname\n w.map(\"firstname\").to(\"First name\")\n w.map(\"lastname\").to(\"Last name\")\n\n w.write()\n ```\n\n The CSV output of the snippet above will be:\n\n```text\nFirst name,Last name,age\nJohn,Smith,40\nDaniel,Nilsson,10\nElla,Fralla,4\n```\n\n## Copyright and License\n\nCopyright (c) 2018 Daniel Furtado. Code released under BSD 3-clause license\n\n## Credits\n\nThis package was created with [Cookiecutter](https://github.com/audreyr/cookiecutter) and the [audreyr/cookiecutter-pypackage](https://github.com/audreyr/cookiecutter-pypackage) project template.\n\n\n# History\n\n### 0.1.0 (2018-11-25)\n\n* First release on PyPI.\n\n### 0.1.1 (2018-11-25)\n\n* Documentation fixes.\n\n### 0.1.2 (2018-11-25)\n\n* Documentation fixes.\n\n### 0.1.3 (2018-11-26)\n\n* Bug fixes\n* Removed the requirement of setting the dataclass init to `True`\n\n### 0.1.5 (2018-11-29)\n\n* Support for parsing datetime values.\n* Better handling when default values are set to `None`\n\n### 0.1.6 (2018-12-01)\n\n* Added support for reader default values from the default property of the `dataclasses.field`.\n* Added support for allowing string values with only white spaces in a class level using the `@accept_whitespaces` decorator or through the `dataclasses.field` metadata.\n* Added support for specifying date format using the `dataclasses.field` metadata.\n\n### 0.1.7 (2018-12-01)\n\n* Added support for default values from `default_factory` in the field's metadata. This allows adding mutable default values to the dataclass properties.\n\n### 1.0.0 (2018-12-16)\n\n* When a data does not pass validation it shows the line number in the CSV file where the data contain errors.\n* Improved error handling.\n* Changed the usage of the `@accept_whitespaces` decorator.\n* Updated documentation.\n\n### 1.0.1 (2019-01-29)\n\n* Fixed issue when parsing headers on a CSV file with trailing white spaces.\n\n### 1.1.0 (2019-02-17)\n\n* Added support for boolean values.\n* Docstrings\n\n### 1.1.1 (2019-02-17)\n\n* Documentation fixes.\n\n### 1.1.2 (2019-02-17)\n\n* Documentation fixes.\n\n### 1.1.3 (2020-03-01)\n\n* Handle properties with init set to False\n* Handle Option type annotation\n\n### 1.2.0 (2021-03-02)\n\n* Introduction of a DataclassWriter\n* Added type hinting to external API\n* Documentation updates\n* Bug fixes\n\n## 1.3.0 (2021-04-10)\n\n* Included stub files\n* check if the CSV file has duplicated header values\n* Fixed issues #22 and #33\n* code cleanup\n\n## 1.4.0 (2021-12-13)\n\n* Bug fixes\n* Support for date types\n\n",
"bugtrack_url": null,
"license": "BSD license",
"summary": "Map CSV data into dataclasses",
"version": "1.4.0",
"project_urls": {
"Homepage": "https://github.com/dfurtado/dataclass-csv"
},
"split_keywords": [
"dataclass",
"dataclasses",
"csv",
"dataclass-csv"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "e5cbd7cb798c4e2eb5b2e8cb6590c694b376a321e9470c6283ab3a0930a801a3",
"md5": "fb769b3a2b54e52a9037c1b23493c354",
"sha256": "e01e99fa75779bb86ae3b9577a06ffc8632061c03115d6a0823137458bfa3d1c"
},
"downloads": -1,
"filename": "dataclass_csv-1.4.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "fb769b3a2b54e52a9037c1b23493c354",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": null,
"size": 16385,
"upload_time": "2021-12-14T07:04:35",
"upload_time_iso_8601": "2021-12-14T07:04:35.141439Z",
"url": "https://files.pythonhosted.org/packages/e5/cb/d7cb798c4e2eb5b2e8cb6590c694b376a321e9470c6283ab3a0930a801a3/dataclass_csv-1.4.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "ca78caaf00f74a195f84181e88084706f3bb1ea94d7ae428bbb0f47630318a9e",
"md5": "e51facaa3b5107b1814c2a51899d0077",
"sha256": "6a9207537ccdc3f381d50c3f5d053ce5a92ef0d61850f7a159e3f52eb79f0402"
},
"downloads": -1,
"filename": "dataclass-csv-1.4.0.tar.gz",
"has_sig": false,
"md5_digest": "e51facaa3b5107b1814c2a51899d0077",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 22544,
"upload_time": "2021-12-14T07:04:36",
"upload_time_iso_8601": "2021-12-14T07:04:36.822047Z",
"url": "https://files.pythonhosted.org/packages/ca/78/caaf00f74a195f84181e88084706f3bb1ea94d7ae428bbb0f47630318a9e/dataclass-csv-1.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2021-12-14 07:04:36",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dfurtado",
"github_project": "dataclass-csv",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "dataclass-csv"
}