CertGrid CertGrid
Hands-on Lab·Python Automation for IT

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

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.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 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.

Before you start

  1. The file this guide works on

  2. Rows as lists

  3. Rows as dictionaries, which is what you want

  4. Converting the columns once, on the way in

  5. Writing a CSV, and the quoting you get for free

  6. And it reads back identically

  7. The newline argument, and what it actually does

  8. Other delimiters, and sniffing one

  9. What a ragged file does

  10. And a column that is not there at all

Official sources