Interview Prep/Python for DevOps/Data formats, HTTP & REST APIs

How do you read and write CSV files properly?

BeginnerConceptData formats, HTTP & REST APIs
Answer
import csv
with open("inventory.csv", newline="", encoding="utf-8") as f:
    for row in csv.DictReader(f):
        print(row["hostname"], row["ip"])

with open("report.csv", "w", newline="", encoding="utf-8") as f:
    w = csv.DictWriter(f, fieldnames=["id", "severity", "detail"])
    w.writeheader()
    w.writerows(findings)

Open with newline="" or you get blank lines on Windows and broken quoted newlines. The csv module handles quoting of commas and quotes in fields; never build CSV with ",".join(...).

All Python for DevOps questions Roadmap: Python for automation

More on Data formats, HTTP & REST APIs