17.4. 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'

17.4.1. SetUp

>>> import csv

17.4.2. Minimal

Data:

>>> 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},
... ]

Usage:

>>> fieldnames = DATA[0].keys()
>>>
>>> with open('/tmp/myfile.csv', mode='wt') as file:
...     result = csv.DictWriter(file, fieldnames)
...     result.writeheader()
...     result.writerows(DATA)
24

Result:

>>> print(open('/tmp/myfile.csv').read())
firstname,lastname,age
Mark,Watney,42
Melissa,Lewis,41
Rick,Martinez,40
Alex,Vogel,42
Beth,Johanssen,29
Chris,Beck,36

17.4.3. Parametrized

Data:

>>> 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},
... ]

Usage:

>>> fieldnames = DATA[0].keys()
>>>
>>> with open('/tmp/myfile.csv', mode='wt', encoding='utf-8') as file:
...     result = csv.DictWriter(file, fieldnames, delimiter=',', quoting=csv.QUOTE_ALL, quotechar='"', lineterminator='\n')
...     result.writeheader()
...     result.writerows(DATA)
29

Result:

>>> print(open('/tmp/myfile.csv').read())
"firstname","lastname","age"
"Mark","Watney","42"
"Melissa","Lewis","41"
"Rick","Martinez","40"
"Alex","Vogel","42"
"Beth","Johanssen","29"
"Chris","Beck","36"

17.4.4. Assignments

# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: CSV DictWriter Fixed
# - Difficulty: easy
# - Lines: 4
# - Minutes: 5

# %% 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:
#    - All fields must be enclosed by double quote `"` character
#    - Use `,` to separate columns
#    - 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:
#    - Wszystkie pola muszą być otoczone znakiem cudzysłowu `"`
#    - Użyj `,` do oddzielenia kolumn
#    - Użyj zakończenia linii Unix `\n`
# 5. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

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

>>> from os import remove
>>> remove(FILE)

>>> 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


FILE = r'_temporary.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},
]

# Using `csv.DictWriter()` save `DATA` to `FILE`
# Open file in your spreadsheet program like:
# Microsoft Excel, Libre Office or Numbers etc.
# Open file in simple in your IDE and simple text editor like:
# Notepad, vim, gedit
# Non functional requirements:
# - All fields must be enclosed by double quote `"` character
# - Use `,` to separate columns
# - Use Unix `\n` line terminator
# type: ContextManager
with open(FILE, mode='wt', encoding='utf-8') as file:
    ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: CSV DictWriter Schemaless
# - Difficulty: medium
# - Lines: 7
# - Minutes: 5

# %% English
# 1. Using `csv.DictWriter()` write variable schema data to `FILE`
# 2. `fieldnames` must be automatically generated from `DATA`
# 3. Non functional requirements:
#    - All fields must be enclosed by double quote `"` character
#    - Use `,` to separate columns
#    - Use `utf-8` encoding
#    - Use Unix `\n` line terminator
#    - 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:
#    - Wszystkie pola muszą być otoczone znakiem cudzysłowu `"`
#    - Użyj `,` do oddzielenia kolumn
#    - Użyj kodowania `utf-8`
#    - Użyj zakończenia linii Unix `\n`
#    - Posortuj `fieldnames` używając `sorted()`
# 4. Uruchom doctesty - wszystkie muszą się powieść

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

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

>>> from os import remove
>>> remove(FILE)

>>> print(result)
"age","firstname","lastname"
"","Mark","Watney"
"41","Melissa",""
"","Rick","Martinez"
"42","","Vogel"
"29","Beth",""
"36","","Beck"
<BLANKLINE>
"""
import csv


FILE = r'_temporary.csv'

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

# Using `csv.DictWriter()` write variable schema data to `FILE`
# `fieldnames` must be automatically generated from `DATA`
# Non functional requirements:
# - All fields must be enclosed by double quote `"` character
# - Use `,` to separate columns
# - Use `utf-8` encoding
# - Use Unix `\n` line terminator
# - Sort `fieldnames` using `sorted()`
# type: ContextManager
with open(FILE, mode='wt', encoding='utf-8') as file:
    ...


# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author

# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -v myfile.py`

# %% About
# - Name: CSV DictWriter Objects
# - Difficulty: medium
# - Lines: 6
# - Minutes: 8

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

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

# %% Hints
# - `vars()`
# - `sorted()`
# - `dict.keys()`
# - `csv.DictWriter()`

# %% Tests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python 3.9+ required'

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

>>> from os import remove
>>> remove(FILE)

>>> print(result)
age,firstname,lastname
40,Mark,Watney
41,Melissa,Lewis
39,Rick,Martinez
<BLANKLINE>
"""

import csv


FILE = r'_temporary.txt'

class User:
    def __init__(self, firstname, lastname, age):
        self.firstname = firstname
        self.lastname = lastname
        self.age = age


DATA = [
    User('Mark', 'Watney', age=40),
    User('Melissa', 'Lewis', age=41),
    User('Rick', 'Martinez', age=39),
]


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