Featurecore
File format modules
Also called csv, json, tomllib, configparser, plistlib, CSV files, JSON, TOML, INI files, property lists.
The standard library reads and writes common data files: csv for comma-separated values, json for JSON, tomllib for TOML, configparser for INI-style files and plistlib for Apple property lists.
- Language
- Python
Synopsis
import csv
import io
import json
import tomllib
text = 'name,langs\nann,"python,go"\nbob,c\n'
rows = list(csv.DictReader(io.StringIO(text, newline="")))
print(rows[0])
print(json.dumps(rows[1]))
config = tomllib.loads('[server]\nport = 8080\nhosts = ["a", "b"]\n')
print(config)
{'name': 'ann', 'langs': 'python,go'}
{"name": "bob", "langs": "c"}
{'server': {'port': 8080, 'hosts': ['a', 'b']}}
Semantics
r1. "The modules described in this chapter parse various miscellaneous file formats that aren’t markup languages and are not related to e-mail."1 The chapter has csv, configparser, tomllib and plistlib. The json module is in another chapter, but it does the same kind of job.
r2. "The so-called CSV (Comma Separated Values) format is the most common import and export format for spreadsheets and databases." "The lack of a well-defined standard means that subtle differences often exist in the data produced and consumed by different applications."2 A dialect describes one variant. Example: csv-quoting.
r3. For csv.reader, "Each row read from the csv file is returned as a list of strings. No automatic data type conversion is performed".2 Example: csv-strings.
r4. A file for csv "should be opened with newline=''".2 Otherwise "newlines embedded inside quoted fields will not be interpreted correctly".2 Example: csv-quoting.
r5. json.dumps turns Python values into JSON text, and json.loads reads it back. A list or a tuple becomes a JSON array, and None becomes null.3 Example: json-round-trip.
r6. "Keys in key/value pairs of JSON are always of the type str. When a dictionary is converted into JSON, all the keys of the dictionary are converted to strings."3 Example: json-int-keys.
r7. tomllib reads TOML. "This module does not support writing TOML."4 tomllib.load needs "a readable and binary file object" and returns a dict.4 A TOML offset date-time becomes a datetime.datetime. Example: toml-types.
r8. configparser reads "a basic configuration language which provides a structure similar to what’s found in Microsoft Windows INI files."5 "Config parsers do not guess datatypes of values in configuration files, always storing them internally as strings."5 Example: ini-file.
r9. plistlib reads and writes "the “property list” files used by Apple, primarily on macOS and iOS. This module supports both binary and XML plist files."6 Example: plist.
Examples
import csv
import io
out = io.StringIO(newline="")
writer = csv.writer(out)
writer.writerow(["id", "note"])
writer.writerow([1, 'says "hi", then\nleaves'])
writer.writerow([2, "plain"])
print(repr(out.getvalue()))
back = list(csv.reader(io.StringIO(out.getvalue(), newline="")))
print(back)
'id,note\r\n1,"says ""hi"", then\nleaves"\r\n2,plain\r\n'
[['id', 'note'], ['1', 'says "hi", then\nleaves'], ['2', 'plain']]
The writer puts quotes around a field with a comma, a quote or a newline, and it doubles each quote in the field. The default excel dialect ends each row with \r\n. The reader undoes all of this, and the number 1 comes back as the string '1' (rule r3).
import json
data = {"name": "ann", "tags": ("a", "b"), "score": 9.5, "admin": False, "boss": None}
text = json.dumps(data, indent=2, sort_keys=True)
print(text)
back = json.loads(text)
print(back["tags"], back == data)
{
"admin": false,
"boss": null,
"name": "ann",
"score": 9.5,
"tags": [
"a",
"b"
]
}
['a', 'b'] False
The tuple comes back as a list, so the result is not equal to the input (rule r5). indent and sort_keys make the text easy to read and to compare.
import tomllib
doc = """
title = "demo"
created = 2026-11-16T09:30:00Z
[owner]
name = "ann"
[[plugins]]
name = "lint"
enabled = true
"""
config = tomllib.loads(doc)
print(config["created"], type(config["created"]).__name__)
print(config["owner"], config["plugins"])
2026-11-16 09:30:00+00:00 datetime
{'name': 'ann'} [{'name': 'lint', 'enabled': True}]
A table becomes a dict, and an array of tables (plugins) becomes a list of dicts (rule r7).
import configparser
parser = configparser.ConfigParser()
parser.read_string("""
[DEFAULT]
Timeout = 30
[server]
port = 8080
debug = no
""")
section = parser["server"]
print(section["port"], type(section["port"]).__name__)
print(section.getint("port"), section.getboolean("debug"), section["timeout"])
print(bool(section["debug"]), list(section))
8080 str
8080 False 30
True ['port', 'debug', 'timeout']
Each value is a string (rule r8). getint and getboolean convert it. bool('no') is True, because any non-empty string is true: "simply passing the value to bool() would do no good since bool('False') is still True."5 The DEFAULT section gives timeout to every section, and "keys in sections are case-insensitive and stored in lowercase".5
import plistlib
data = {"name": "demo", "version": 3, "items": [1, 2]}
xml = plistlib.dumps(data)
print(xml.splitlines()[0])
binary = plistlib.dumps(data, fmt=plistlib.FMT_BINARY)
print(binary[:8], plistlib.loads(binary) == data, plistlib.loads(xml) == data)
b'<?xml version="1.0" encoding="UTF-8"?>'
b'bplist00' True True
dumps writes XML by default. loads finds the format from the data (rule r9).
Versions
Since 3.11
is new, "with support for TOML 1.0.0."4
Since 3.15
supports TOML 1.1.0.4
Pitfalls
This code runs but gives a wrong answer on purpose.
import csv
import io
rows = list(csv.reader(io.StringIO("9\n10\n100\n", newline="")))
values = [row[0] for row in rows]
print(sorted(values), max(values))
print(sorted(int(v) for v in values))
['10', '100', '9'] 9
[9, 10, 100]
The values are strings, so they sort as text and '9' is the largest (rule r3). Convert each field to the type you need.
This code runs but gives a wrong answer on purpose.
import json
counts = {1: "one", 2: "two"}
back = json.loads(json.dumps(counts))
print(back, back == counts)
{'1': 'one', '2': 'two'} False
The integer keys come back as strings (rule r6). Convert them after loading, or store the data as a list of pairs.
This code raises TypeError, on purpose.
import json
print(json.dumps({"tags": {"a", "b"}}))
JSON has no set type, so dumps raises TypeError. Convert the set to a sorted list, or pass a default function that does it.
Comparison
These formats store plain data: strings, numbers, lists and maps. For arbitrary Python objects, see pickle and object serialization, which is not safe for untrusted input. For XML, see XML and HTML modules. For JSON input from outside, the docs warn: "A malicious JSON string may cause the decoder to consume considerable CPU and memory resources. Limiting the size of data to be parsed is recommended."3
References
- File Formats, Python 3.15 documentation, File Formats
- csv, CSV File Reading and Writing, Python 3.15 documentation, Introduction; csv.reader(); csv.writer(); Footnotes
- json, JSON encoder and decoder, Python 3.15 documentation, Introduction; Basic Usage; Encoders and Decoders
- tomllib, Parse TOML files, Python 3.15 documentation, Introduction; load(); Conversion Table
- configparser, Configuration file parser, Python 3.15 documentation, Introduction; Quick Start; Supported Datatypes
- plistlib, Generate and parse Apple .plist files, Python 3.15 documentation, Introduction
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag 0C5Q0P Edit this page History Report an error Markdown