Python for data

Lesson 2 of 15

Lesson ยท 15 min

Rows from a CSV file

Tables in text files

A CSV file is a table in a text file. The letters mean "comma-separated values". Each line is one row. Commas separate the values of a row. The first row is usually the header row. It has the names of the columns.

trip_id,date,start,end,minutes,rider
1001,2026-05-04,Harbor Gate,Fish Market,12,member

Spreadsheet programs and databases can save a table as a CSV file, so you will see this format often.

Do not split the lines yourself

line.split(",") looks like the right tool. It breaks when a value has a comma in it. A CSV file puts such a value in quotation marks:

1004,2026-05-05,"Pier 4, West Side",Lighthouse,41,casual

This row has 6 values, and split(",") gives 7 pieces. The csv module reads the row correctly.

Modules

A module is a file of Python code that other code can use. Python comes with many modules. The line import csv loads the module named csv. After that line, you use a tool of the module with the name of the module and a dot, for example csv.reader. Write the import lines at the top of your file.

Read the rows

import csv

with open("pets.csv", newline="", encoding="utf-8") as file:
    rows = list(csv.reader(file))

print(rows[0])   # ['name', 'age']
print(rows[1])   # ['Biscuit', '4']
  • csv.reader(file) gives each row as a list of values.
  • list( ) collects all the rows in one list. Do this inside the with block, while the file is open.
  • newline="" belongs in every open call for a CSV file. The csv module then handles the ends of the lines itself.

The first row is the header row. A slice gives the rows after it.

header = rows[0]
data = rows[1:]

Every value is text

Look at the output above. The age is '4', with quotation marks. A CSV file has only text, and Python does not guess which values are numbers. You convert them.

Code Result
int("12") 12
float("3.5") 3.5
int("12") + 1 13
"12" + 1 An error. Python cannot add text and a number.
int("twelve") An error. The text is not a number.

Find a column by its name

The position of a column changes when someone adds a column to the file. The name stays the same. The index method gives the position of a value in a list.

position = header.index("age")   # 1

ages = []
for row in data:
    ages.append(int(row[position]))

Your turn

The file trips.csv has 10 trips. One station has a comma in its name. Do the tasks in order.

Tasks

5 to do

  1. Task 1, not checked yet. Import the csv module. Read trips.csv with csv.reader into a list named rows.

  2. Task 2, not checked yet. Make a variable header with the first row of rows. Make a variable data with all the other rows.

  3. Task 3, not checked yet. Make a list named starts with the start station of each trip, in the order of the file.

  4. Task 4, not checked yet. Make a variable total_minutes with the sum of the column minutes, as a whole number.

  5. Task 5, not checked yet. Write a function column(rows, name). rows is a list like yours, with the header row first. The function returns the list of the values in the column name, without the header.

Task 1 of 5

Import the csv module. Read trips.csv with csv.reader into a list named rows.

All tasks

Ctrl+Enter runs your code. Ctrl+Shift+Enter checks it. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.