Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
Loading…
Code Lab is in development. New lessons and improvements are on the way. Progress can be deleted.
PYTHON FILES / CHAPTER 05 / Learn / LESSON 07
CSV is a table in text. Fields may contain commas inside quotes — so do not split(",").
import csv with open("items.csv", encoding="utf-8", newline="") as f: reader = csv.DictReader(f) total = 0 for row in reader: total += int(row["qty"]) print(total)
DictReader yields a dict per row; keys come from the header row. newline="" is recommended for csv.
--- items.csv ---
name,qty,unit
"milk, 2%",3,L
bread,1,pc
Learn: why not split(",")
AGGREGATE IDLE
›