- Lab
- Data

Importing Text Files in Python Hands-on Practice
In this lab, you'll gain practical skills in managing text files with Python. Starting with basic file operations, you'll progress to reading and analyzing CSV files using Python's CSV module. You'll then explore data handling with Numpy, learning to specify data types and load specific columns.

Path Info
Table of Contents
-
Challenge
Working with Text Files
Jupyter Guide
To get started, open the file on the right entitled "Step 1...". You'll complete each task for Step 1 in that Jupyter Notebook file. Remember, you must run the cells
(ctrl/cmd(β) + Enter)
for each task before moving onto the next task in the Jupyter Notebook. Continue until you have completed all tasks in this step. Then when you are ready to move onto the next step, you'll come back and click on the file for the next step until you have completed all tasks in all steps of the lab.
Working with Text Files
To review the concepts covered in this step, please refer to the Importing Text Files module of the Importing Text Files in Python course.
Knowing how to work with text files forms the basis of data processing in Python. This step will help you understand how to open, read, and close text files in Python.
In this step, we will practice opening, reading, and closing text files in Python. We will use the open function to open a file, read its contents, and then close it. We will also explore the different methods for reading a text file, such as read, readline, and readlines. The goal of this step is to familiarize you with the process of working with text files in Python.
Task 1.1: Opening a Text File
Open the 'lyrics.txt' file using the open function in Python. Assign the opened file to a variable named 'file'. Print a message indicating the file has been opened, using the 'name' attribute of the file object to display the name of the file.
π Hint
Use the
open()
function with the filename as the first argument and the mode (e.g., 'r' for reading) as the second argument. Assign this to a variable. To print the filename, use thename
attribute of the file object.π Solution
file = open('lyrics.txt', 'r') print(f"File '{file.name}' opened successfully")
Task 1.2: Reading a Text File
Read the contents of the 'file' using the read method and assign the result to a variable named 'data'. Print the content of 'data'. Use the 'mode' attribute of the file object to indicate the mode in which the file is opened.
π Hint
Utilize the
read()
method on your file object to read the contents. Assign this to a variable. Display the mode of the file using themode
attribute of the file object.π Solution
data = file.read() print(f"Reading file in '{file.mode}' mode:") print(data)
Task 1.3: Closing a Text File
Close the 'file' using the close method in Python. Print a message indicating the file has been closed, using the 'closed' attribute of the file object to check if the file is closed.
π Hint
Invoke the
close()
method on the file object. Then, check if the file is closed using theclosed
attribute, which returns a boolean.π Solution
file.close() print(f"File closed: {file.closed}")
Task 1.4: Reading a Text File Line by Line with file.readline()
Open the 'lyrics.txt' file again and read it line by line using the readline method. Print each line as you read it. Use a
with
statement to automatically handle file closing.π Hint
Implement a
with
statement to open the file. Inside this with block, use a loop with thereadline()
method to read and print each line. The loop should continue until no more lines are returned.π Solution
with open('lyrics.txt', 'r') as file: print(f"File '{file.name}' opened for line-by-line reading") line = file.readline() while line: print(line) line = file.readline()
Task 1.5: Reading All Lines at Once
Open the 'lyrics.txt' file again and read all lines at once using the readlines method. Assign the result to a variable named 'lines'. Print the 'lines' variable. Use a
with
statement for this task.π Hint
Use a
with
statement to open the file. Inside the block, apply thereadlines()
method to read all lines into a list and then print this list.π Solution
with open('lyrics.txt', 'r') as file: print(f"File '{file.name}' opened for reading all lines at once") lines = file.readlines() print(lines)
Task 1.6: Iterating Over a File with a For Loop
Open the 'lyrics.txt' file once more and use a for loop to iterate over each line in the file. Print each line as you iterate over it. Utilize a
with
statement to manage the file opening and closing.π Hint
Use the
with
statement to open the file. Then, create a for loop that directly iterates over the file object to print each line.π Solution
with open('lyrics.txt', 'r') as file: print(f"File '{file.name}' opened for iteration using a for loop") for line in file: print(line)
-
Challenge
Loading and Reading Flat Files
Loading and Reading Flat Files
To review the concepts covered in this step, please refer to the Loading and Reading Flat Files module of the Importing Text Files in Python course.
Knowing how to load and read flat files allows you to work with structured data in Python. This step will help you understand the structure of flat files and how to read them using the CSV Reader module.
In this step, we will practice loading and reading flat files in Python. We will explore the structure of flat files, the different types of flat files, and how to read them using the CSV Reader module. The goal of this step is to familiarize you with the process of working with flat files in Python.
Task 2.1: Importing the CSV Reader Module
Before we can start working with flat files, we need to import the necessary module. In this task, you will import Python's built-in CSV module, which is designed for reading and writing CSV files.
π Hint
Use the
import
keyword to import the CSV module.π Solution
import csv
Task 2.2: Opening the CSV File
Now that we have imported the CSV module, we can open our CSV file. In this task, you will use Python's built-in
open()
function to open the 'World_Bank-Fictional.csv' file.π Hint
Use the
open()
function with the file name as a string. Remember to specify the correct mode for reading.π Solution
file = open('World_Bank-Fictional.csv', 'r')
Task 2.3: Reading the CSV File Using CSV Reader
After opening the file, we will use the CSV Reader to read the file. In this task, you will create a CSV reader object and iterate over the rows of the CSV file, displaying them. Close the file after you have printed the rows.
π Hint
Use the
csv.reader()
function and pass the file object to it. Then, iterate over the rows using a for loop.π Solution
csv_reader = csv.reader(file) for row in csv_reader: print(row) file.close()
Task 2.4: Analyzing the Structure and Content of the CSV Data
In this task, you will analyze the structure and content of the data in the 'World_Bank-Fictional.csv' file. You'll use the CSV Reader to read the file and then write code to understand the type of data it contains. Use a
with
statement to handle opening and closing of the file. Determine the number and name of columns, and count the number of rows. Print the data in the first row of each column.π Hint
The following steps should help you accomplish the task.
- Open the CSV file using
with open('filename.csv', 'r') as file:
. - Read the file using
csv.reader(file)
. - Fetch the first row with
next(csv_reader)
for headers. - Increment a counter in a loop to count rows.
- Access elements of the first row for column data.
- Print headers, first row data, and total row count.
π Solution
import csv # Open the CSV file with open('World_Bank-Fictional.csv', 'r') as file: csv_reader = csv.reader(file) # Reading the headers headers = next(csv_reader) print("Headers:", headers) # Initializing a counter for rows row_count = 0 # Analyzing the data in each row for row in csv_reader: row_count += 1 # Print first row data if row_count == 1: print("First row data:", row) print("Total number of rows:", row_count) # Outputs headers, first row data, and row count.
- Open the CSV file using
-
Challenge
Importing Text Files with Numpy
Importing Text Files with Numpy
To review the concepts covered in this step, please refer to the Import Text File with Numpy module of the Importing Text Files in Python course.
Importing text files with Numpy is important because it allows you to work with numerical data in Python. This step will help you understand how to load data from a file using Numpy and how to specify the data type of the values loaded using dtype.
In this step, we will practice importing text files with Numpy. We will explore how to load data from a file using Numpy, how to specify the data type of the values loaded using dtype, and how to load specific columns using usecols. The goal of this step is to familiarize you with the process of importing text files with Numpy.
Task 3.1: Import Numpy
Before we can start working with Numpy, we need to import it. Import the Numpy library using the standard alias 'np'.
π Hint
Use the
import
keyword followed by the library name andas
keyword to give it an alias. For example,import numpy as np
.π Solution
import numpy as np
Task 3.2: Load Data from CSV File
Now that we have Numpy imported, we can use it to load data from our CSV file. Use the
np.genfromtxt
function to load the data from the 'World_Bank-Fictional.csv' file. For now, just load the data without specifying any data types or columns. Print the data. You'll see some NaN values and everything will be of dtype float.π Hint
Use the
np.genfromtxt
function with the file path as the first argument. For example,np.genfromtxt('file_path.csv', delimiter=',', skip_header=1)
.π Solution
data = np.genfromtxt('World_Bank-Fictional.csv', delimiter=',', skip_header=1) print(data)
Task 3.3: Specify Data Types
Specify the data types for each column when loading data from a CSV file, using the
dtype
argument in thenp.genfromtxt()
function. Print the data after it has been loaded. Notice the difference fromTask 3.2
.The table below outlines the columns and their corresponding data types in order:
| Column Name | Data Type | |-------------------|-----------| | Country | object | | Year | int64 | | Population | int64 | | GDP | int64 | | GDP_Growth | float64 | | Inflation | float64 | | Life_Expectancy | float64 | | Literacy_Rate | float64 | | Unemployment_Rate | float64 |
π Hint
Use the
dtype
parameter in thenp.genfromtxt
function with the following formatted argument:dtype=['object', 'int64', 'int64', 'int64', 'float64', 'float64', 'float64', 'float64', 'float64']
π Solution
# Load the data with specified data types data = np.genfromtxt('World_Bank-Fictional.csv', delimiter=',', skip_header=1, dtype=['object', 'int64', 'int64', 'int64', 'float64', 'float64', 'float64', 'float64', 'float64']) # Display the data print(data)
Task 3.4: Load Specific Columns
Sometimes we only want to load specific columns from our data. Reload the data from the CSV file, but this time only load the 'Country', 'Year', and 'Population' columns. Use the dtpye argument to load the strings and numbers correctly. Print the data.
π Hint
Use the
usecols
parameter in thenp.genfromtxt
function and pass a list of column indices. For example,np.genfromtxt('file_path.csv', delimiter=',', skip_header=1, usecols=[0, 1, 2], dtype = ["your_dtypes_here"])
.π Solution
data = np.genfromtxt('World_Bank-Fictional.csv', delimiter=',', skip_header=1, usecols=[0, 1, 2], dtype=['object', 'int64', 'int64']) print(data)
-
Challenge
Importing Text Files into Pandas
Importing Text Files into Pandas
To review the concepts covered in this step, please refer to the Import Text File into Pandas module of the Importing Text Files in Python course.
Importing text files into Pandas allows you to work with tabular data in Python. This step will help you understand how to load data from a file using Pandas and how to handle missing values in a dataframe using na_filter.
In this step, we will practice importing text files into Pandas. We will explore how to load data from a file using Pandas, how to handle missing values in a dataframe using na_filter, and how to parse dates in a dataframe using parse_dates. The goal of this step is to familiarize you with the process of importing text files into Pandas.
Task 4.1: Importing Pandas Library
Import the pandas library as pd.
π Hint
Use the
import
keyword followed by the library name andas
keyword to give it a short alias.π Solution
import pandas as pd
Task 4.2: Loading Data from a CSV File
Load the data from the 'World_Bank-Fictional.csv' file into a pandas DataFrame and display the DataFrame.
π Hint
Use the
pd.read_csv()
function and pass the file path as a string to it.π Solution
df = pd.read_csv('World_Bank-Fictional.csv') df
Task 4.3: Parsing Dates
Load the data once more but this time, parse the 'Year' column as dates and display the DataFrame.
π Hint
Use the
pd.read_csv()
function again but this time, set theparse_dates
parameter to a list containing the column name 'Year'.π Solution
df = pd.read_csv('World_Bank-Fictional.csv', parse_dates=['Year']) df
What's a lab?
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Provided environment for hands-on practice
We will provide the credentials and environment necessary for you to practice right within your browser.
Guided walkthrough
Follow along with the authorβs guided walkthrough and build something new in your provided environment!
Did you know?
On average, you retain 75% more of your learning if you get time for practice.