Course 2.1Lesson 3 of 5
CSV, comma separated values, is a table written as text, one row on each line with commas between the values, and often a header row of names. The csv module reads it: reader gives each row as a list of texts, and DictReader uses the header to give each row as a dictionary. Every value is text, so numbers must be converted. writer and writerow write rows, and the module protects values that hold commas or quotation marks, which split on a comma cannot do. Open the file with newline equal to an empty text for csv. io.StringIO lets a text behave like a file, which is useful for tests.
Try the code and the simulations beside the lesson.
After this lesson you can
split(",") is not enough.A spreadsheet is a table: rows and columns. How do you save it as a file that any program can read? The simplest answer is CSV: comma-separated values. Each row is a line. The values of a row are separated by commas. Often the first row is a header: the names of the columns.
Look at the left box of the picture: three students, one row each. You can open a CSV file in a text editor, and in any spreadsheet program.
Python has a module for it: csv. We use io.StringIO to make a text behave like a file, so that the examples run alone.
Predict first. What is rows[1][1] + rows[2][1]? Then run it.
csv.reader gives each row as a list. rows[0] is the header, ["name", "mark", "city"].rows[1][1] is "72", not 72. So "72" + "85" is "7285". This is the same trap as with input(): convert before you calculate.csv.DictReader uses the header to give each row as a dictionary: row["name"], row["mark"]. This is much easier to read than row[1], and it does not break if somebody adds a column. Use it when the file has a header.DictReader lets the program say row["maths"], by name. If someone adds a column to the file, the program still finds the right one.
When the rows are dictionaries, the usual jobs are short.
[int(row["mark"]) for row in rows].[row["name"] for row in rows if int(row["mark"]) > 70].row["city"] as the key.max(rows, key=...) with a key function that returns the mark as a number.Notice that you must convert inside the key too: max of the texts "9" and "10" says that "9" is bigger, because text is compared letter by letter.
int() or float().split(",") for CSV. Use the csv module.newline="". Blank lines may appear between the rows on some computers.DictReader takes the first data row as the header.row["mark"] is a KeyError or None.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
csv.reader gives lists. csv.DictReader gives dictionaries from the header.newline="" in open, and the csv module instead of split(",").Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
row[1] when you read Asha,72 with csv.reader?DictReader easier to read than reader?split(",") give a wrong answer for a CSV line?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.