Python for data

Lesson 1 of 15

Lesson ยท 14 min

Read a text file

Data is in files

In the Python course, the data was in the code. Real data is in files: a list of names, a table of sales, the answers of a survey. In this course, you read files with Python, and you answer questions about the data in them.

Every lesson uses the data of one made-up bike share service in a small harbor town. People take a bike at one station and leave it at another station.

This course uses only the tools that come with Python. Many teams also use a library named pandas for this work. The ideas are the same: you read rows, you filter them, you group them, and you join tables.

The files of a lesson

A lesson can have data files next to main.py. Each file has a tab above the editor. Select a tab to read the file. A lock marks a file that you cannot change. Your code in main.py opens a file by its name.

Open a file and read it

with open("notes.txt", encoding="utf-8") as file:
    text = file.read()

print(text)
  • open takes the name of the file and gives a file object. as file gives that object a name.
  • encoding="utf-8" tells Python how the file stores its characters. Most text files use UTF-8. Write it each time.
  • The lines with an indent are the block of with. Python closes the file when the block ends.
  • file.read() gives all the text of the file as one string.

From text to lines

The end of a line in a file is a special character. In Python, you write it as \n. The method splitlines() cuts a text at these characters and gives a list of lines.

text = "red\ngreen\n\nblue\n"
print(text.splitlines())   # ['red', 'green', '', 'blue']

The third item is an empty text, because the text has an empty line.

You can also read a file line by line. A for loop over the file object gives one line at a time. Each line still has its \n at the end.

with open("notes.txt", encoding="utf-8") as file:
    for line in file:
        print(line.strip())

Clean each line

Files that people write by hand have extra spaces and empty lines. You know strip() from the Python course. It removes the spaces at both ends, and it also removes \n. A line that had only spaces becomes the empty text "".

names = []
for line in lines:
    name = line.strip()
    if name != "":
        names.append(name)

Count while you loop

enumerate gives a number together with each item. start=1 makes the first number 1.

for number, color in enumerate(["red", "green"], start=1):
    print(number, color)
# 1 red
# 2 green

Your turn

The file stations.txt has the names of the stations. One line is empty, and one name has spaces in front of it. Do the tasks in order. Each task uses the result of the task before it.

Tasks

5 to do

  1. Task 1, not checked yet. Open stations.txt and read all of its text into a variable named text.

  2. Task 2, not checked yet. Make a variable lines. It is the list of the lines of text. Use the splitlines method.

  3. Task 3, not checked yet. Make a list named stations with the name of each station. Remove the spaces around each name, and leave out the empty line.

  4. Task 4, not checked yet. Print one line for each station: its number, a dot, a space, and the name. The first line is 1. Harbor Gate.

  5. Task 5, not checked yet. Write a function read_names(file_name). It opens the file with that name and returns the list of the names in it. It removes the spaces around each name, and it leaves out empty lines.

Task 1 of 5

Open stations.txt and read all of its text into a variable named text.

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.