Python CSV and Tabular Data
CSV is what an inventory, an export or a report arrives as. The csv module handles the parts that go wrong if you use split(",") - quoted fields containing commas, embedded quotes, and line endings - and DictReader gives you rows keyed by column name. Everything comes back as a string, so the useful pattern is converting the columns once on the way in. And a ragged file produces no error at all, which is the part worth knowing.
Files and Data Formats Guide 11 of 39 Beginner
- Control nodeUbuntu 26.04 LTS
- Python3.14.4
- requests2.34.2
- paramiko5.0.0
- TimeAbout 17 min
The csv module is unchanged across Python 3. The newline="" argument is documented as required and, as this guide demonstrates, makes no difference on Linux - it matters on Windows.
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Control node - every script in this path runs here | 2 Core | 4 GB | 50 GB |
This guide includes
Use this when the inventory arrives as a spreadsheet export. This matters because a ragged row does not raise and a missing column does - so the failure you expect to be told about is the one CSV stays quiet on.
- reading rows as lists, then as dictionaries, which is what you actually want
- converting the numeric columns once, on the way in
- writing a CSV, and getting the quoting right for free
- learning what the
newlineargument really does - handling other delimiters, ragged rows, and the column that is not there at all
Before you start
-
The file this guide works on
-
Rows as lists
-
Rows as dictionaries, which is what you want
-
Converting the columns once, on the way in
-
Writing a CSV, and the quoting you get for free
-
And it reads back identically
-
The newline argument, and what it actually does
-
Other delimiters, and sniffing one
-
What a ragged file does
-
And a column that is not there at all