8.8. CSV DictWriter

  • Writes iterable of dicts (eg. list[dict]) to CSV file

  • csv.DictWriter()

  • Remember to add mode='w' to open() function

  • Default encoding is encoding='utf-8'

8.8.1. SetUp

>>> import csv
>>> from pathlib import Path

8.8.2. Minimal

>>> DATA = [
...     {'sepal_length': 5.4, 'sepal_width': 3.9, 'petal_length': 1.3, 'petal_width': 0.4, 'species': 'setosa'},
...     {'sepal_length': 5.9, 'sepal_width': 3.0, 'petal_length': 5.1, 'petal_width': 1.8, 'species': 'virginica'},
...     {'sepal_length': 6.0, 'sepal_width': 3.4, 'petal_length': 4.5, 'petal_width': 1.6, 'species': 'versicolor'},
... ]
>>>
>>> header = DATA[0].keys()
>>>
>>> with open('/tmp/myfile.csv', mode='w') as file:
...     result = csv.DictWriter(file, fieldnames=header)
...     result.writeheader()
...     result.writerows(DATA)
59

Result:

>>> print(Path('/tmp/myfile.csv').read_text())
sepal_length,sepal_width,petal_length,petal_width,species
5.4,3.9,1.3,0.4,setosa
5.9,3.0,5.1,1.8,virginica
6.0,3.4,4.5,1.6,versicolor

8.8.3. Parametrized

>>> DATA = [
...     {'sepal_length': 5.4, 'sepal_width': 3.9, 'petal_length': 1.3, 'petal_width': 0.4, 'species': 'setosa'},
...     {'sepal_length': 5.9, 'sepal_width': 3.0, 'petal_length': 5.1, 'petal_width': 1.8, 'species': 'virginica'},
...     {'sepal_length': 6.0, 'sepal_width': 3.4, 'petal_length': 4.5, 'petal_width': 1.6, 'species': 'versicolor'},
... ]
>>>
>>>
>>> header = DATA[0].keys()
>>>
>>> with open('/tmp/myfile.csv', mode='w', encoding='utf-8') as file:
...     result = csv.DictWriter(file, fieldnames=header, quotechar='"', delimiter=';', quoting=csv.QUOTE_ALL)
...     result.writeheader()
...     result.writerows(DATA)
69

Result:

>>> print(Path('/tmp/myfile.csv').read_text())
"sepal_length";"sepal_width";"petal_length";"petal_width";"species"
"5.4";"3.9";"1.3";"0.4";"setosa"
"5.9";"3.0";"5.1";"1.8";"virginica"
"6.0";"3.4";"4.5";"1.6";"versicolor"

8.8.4. Assignments

Code 8.31. Solution
"""
* Assignment: CSV DictWriter Fixed
* Complexity: easy
* Lines of code: 4 lines
* Time: 5 min

English:
    1. Using `csv.DictWriter()` save `DATA` to `FILE`
    2. Open file in your spreadsheet program like:
       Microsoft Excel, Libre Office or Numbers etc.
    3. Open file in simple in your IDE and simple text editor like:
       Notepad, vim, gedit
    4. Non functional requirements:
        a. All fields must be enclosed by double quote `"` character
        b. Use `,` to separate columns
        d. Use Unix `\n` line terminator
    5. Run doctests - all must succeed

Polish:
    1. Za pomocą `csv.DictWriter()` zapisz `DATA` do `FILE`
    2. Spróbuj otworzyć plik w arkuszu kalkulacyjnym tj.
       Microsoft Excel, Libre Office lub Numbers itp
    3. Spróbuj otworzyć plik w IDE i prostym edytorze tekstu tj.
       Notepad, vim lub gedit
    4. Wymagania niefunkcjonalne:
        a. Wszystkie pola muszą być otoczone znakiem cudzysłowu `"`
        b. Użyj `,` do oddzielenia kolumn
        d. Użyj zakończenia linii Unix `\n`
    5. Uruchom doctesty - wszystkie muszą się powieść

Hint:
    * For Python before 3.8: `dict(OrderedDict)`

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from os import remove
    >>> result = open(FILE).read()
    >>> remove(FILE)

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is str, \
    'Variable `result` has invalid type, should be str'

    >>> print(result)   # doctest: +NORMALIZE_WHITESPACE
    "firstname","lastname","age"
    "Mark","Watney","42"
    "Melissa","Lewis","41"
    "Rick","Martinez","40"
    "Alex","Vogel","42"
    "Beth","Johanssen","29"
    "Chris","Beck","36"
    <BLANKLINE>
"""
import csv


DATA = [
    {'firstname': 'Mark', 'lastname': 'Watney', 'age': 42},
    {'firstname': 'Melissa', 'lastname': 'Lewis', 'age': 41},
    {'firstname': 'Rick', 'lastname': 'Martinez', 'age': 40},
    {'firstname': 'Alex', 'lastname': 'Vogel', 'age': 42},
    {'firstname': 'Beth', 'lastname': 'Johanssen', 'age': 29},
    {'firstname': 'Chris', 'lastname': 'Beck', 'age': 36},
]

FILE = r'_temporary.csv'

# Write DATA to FILE, generate header from DATA
# type: ContextManager
with open(FILE, mode='w') as file:
    ...


Code 8.32. Solution
"""
* Assignment: CSV DictWriter Schemaless
* Complexity: medium
* Lines of code: 7 lines
* Time: 5 min

English:
    1. Using `csv.DictWriter()` write variable schema data to `FILE`
    2. `fieldnames` must be automatically generated from `DATA`
    3. Non functional requirements:
        a. All fields must be enclosed by double quote `"` character
        b. Use `,` to separate columns
        c. Use `utf-8` encoding
        d. Use Unix `\n` line terminator
        e. Sort `fieldnames` using `sorted()`
    4. Run doctests - all must succeed

Polish:
    1. Za pomocą `csv.DictWriter()` zapisz dane o zmiennej strukturze do `FILE`
    2. `fieldnames` musi być generowane automatycznie na podstawie `DATA`
    3. Wymagania niefunkcjonalne:
        a. Wszystkie pola muszą być otoczone znakiem cudzysłowu `"`
        b. Użyj `,` do oddzielenia kolumn
        c. Użyj kodowania `utf-8`
        d. Użyj zakończenia linii Unix `\n`
        e. Posortuj `fieldnames` używając `sorted()`
    4. Uruchom doctesty - wszystkie muszą się powieść

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from os import remove
    >>> result = open(FILE).read()
    >>> remove(FILE)

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is str, \
    'Variable `result` has invalid type, should be str'

    >>> print(result)
    "petal_length","petal_width","sepal_length","sepal_width","species"
    "","","5.1","3.5","setosa"
    "4.1","1.3","","","versicolor"
    "","1.8","6.3","","virginica"
    "","0.2","5.0","","setosa"
    "4.1","","","2.8","versicolor"
    "","1.8","","2.9","virginica"
    <BLANKLINE>
"""
import csv


DATA = [
    {'sepal_length': 5.1, 'sepal_width': 3.5, 'species': 'setosa'},
    {'petal_length': 4.1, 'petal_width': 1.3, 'species': 'versicolor'},
    {'sepal_length': 6.3, 'petal_width': 1.8, 'species': 'virginica'},
    {'sepal_length': 5.0, 'petal_width': 0.2, 'species': 'setosa'},
    {'sepal_width': 2.8, 'petal_length': 4.1, 'species': 'versicolor'},
    {'sepal_width': 2.9, 'petal_width': 1.8, 'species': 'virginica'},
]

FILE = r'_temporary.csv'


# Write DATA to FILE, generate header from DATA
# type: ContextManager
with open(FILE, mode='w', encoding='utf-8') as file:
    ...


Code 8.33. Solution
"""
* Assignment: CSV DictWriter Objects
* Complexity: medium
* Lines of code: 6 lines
* Time: 8 min

English:
    1. Using `csv.DictWriter()` save data to `FILE`
    2. Non-functional requirements:
        a. Use `,` to separate columns
        b. Use `utf-8` encoding
        c. Use Unix `\n` line terminator
        d. sort header (fieldnames)
    3. Run doctests - all must succeed

Polish:
    1. Za pomocą `csv.DictWriter()` zapisz dane do `FILE`
    2. Wymagania niefunkcjonalne:
        a. Użyj `,` do oddzielenia kolumn
        b. Użyj kodowania `utf-8`
        c. Użyj zakończenia linii Unix `\n`
        d. posortuj nagłówek (fieldnames)
    3. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `vars()`

Tests:
    >>> import sys; sys.tracebacklimit = 0
    >>> from os import remove
    >>> result = open(FILE).read()
    >>> remove(FILE)

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'
    >>> assert type(result) is str, \
    'Variable `result` has invalid type, should be str'

    >>> print(result)
    petal_length,petal_width,sepal_length,sepal_width,species
    1.4,0.2,5.1,3.5,setosa
    5.1,1.9,5.8,2.7,virginica
    1.4,0.2,5.1,3.5,setosa
    4.1,1.3,5.7,2.8,versicolor
    5.6,1.8,6.3,2.9,virginica
    4.5,1.5,6.4,3.2,versicolor
    <BLANKLINE>
"""

import csv


class Iris:
    def __init__(self, sepal_length, sepal_width,
                 petal_length, petal_width, species):
        self.sepal_length = sepal_length
        self.sepal_width = sepal_width
        self.petal_length = petal_length
        self.petal_width = petal_width
        self.species = species


DATA = [
    Iris(5.1, 3.5, 1.4, 0.2, 'setosa'),
    Iris(5.8, 2.7, 5.1, 1.9, 'virginica'),
    Iris(5.1, 3.5, 1.4, 0.2, 'setosa'),
    Iris(5.7, 2.8, 4.1, 1.3, 'versicolor'),
    Iris(6.3, 2.9, 5.6, 1.8, 'virginica'),
    Iris(6.4, 3.2, 4.5, 1.5, 'versicolor'),
]

FILE = r'_temporary.txt'


# Write DATA to FILE, generate header from DATA
# type: ContextManager
with open(FILE, mode='w', encoding='utf-8') as file:
    ...