- Lab
- Data

Reading and Writing CSV Files in Python Hands-on Practice
In the this lab, you'll gain practical skills in handling various file operations using Python. First, you'll learn basic file handling techniques, including opening, reading, writing, and appending data in text files. Then, you'll explore file management using the OS module, checking file existence and performing deletions. The lab progresses to reading and writing CSV files using Python's CSV module, where you'll handle standard and dictionary-based CSV operations. Finally, you'll delve into advanced data manipulation with Pandas and NumPy, learning to read, concatenate, and manipulate data from CSV files. This comprehensive lab equips you with essential Python skills for effective data handling and manipulation.

Path Info
Table of Contents
-
Challenge
File Handling in Python
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.
File Handling in Python
To review the concepts covered in this step, please refer to the Opening and Modifying Files module of the Reading and Writing CSV Files in Python course.
File handling is important because it allows us to read, write, and modify data stored in files. In this step, we will practice opening a file, reading its content, and writing new data to it.
In this step, we will practice the basics of file handling in Python. We will use the
open
function to open a file, read its content using theread
method, and write new data using thewrite
method. We will also explore the use of different modes such as read mode (r), write mode (w), and create mode (x). Finally, we'll print a file one line at a time usingreadlines
and afor
loop. We'll be working with theexample_data.txt
file.
Task 1.1: Opening a File
Open the
example_data.txt
file in read mode and print its content. Close the file after printing the data.π Hint
Use the
read
method to read the content of the file and theprint
function to print it.π Solution
file = open('example_data.txt', 'r') print(file.read()) file.close()
Task 1.2: Writing to a File
Open the
example_data.txt
file in append mode and write 'Hello, World!' to it. Be sure to close the file to save your changes.π Hint
Use the
file.write
method to write 'Hello, World!' to the file. Be sure to open the file with'a'
argument to specify append mode, so that we add to the file instead of overwriting it.π Solution
file = open('example_data.txt', 'a') file.write('Hello, World!') file.close()
Task 1.3: Creating a File
Create a new file named
new_file.txt
and write 'This is a new file.' to it. Be sure to close the file to save your changes.π Hint
Use the
open
function with the 'x' mode to create a new file. Then use thewrite
method to write to it.π Solution
file = open('new_file.txt', 'x') file.write('This is a new file.') file.close()
Task 1.4: Reading a File Line by Line
Open the
example_data.txt
file in read mode and print its content line by line with a line number before each line. Close the file after reading each line. Use afor
loop with a counter that increments with each line to get the line number.π Hint
Use the
readlines
method to get a list of lines from the file. Incrementing a counter in a for loop will follow this patter:counter = 1 for foo in bar: print(counter) print(foo) counter += 1
π Solution
file = open('example_data.txt', 'r') counter = 1 for line in file.readlines(): print(counter) print(line) counter += 1 file.close()
-
Challenge
Reading and Writing Files
Reading and Writing Files
To review the concepts covered in this step, please refer to the Opening and Modifying Files module of the Reading and Writing CSV Files in Python course.
Understanding the OS module is important because it provides a way of using operating system dependent functionality. In this step, we will practice checking if a file exists and deleting it using the OS module.
In this step, we will practice using the OS module to interact with the file system. We will use the
os.path.exists()
function to check if a file exists (comma_separated.csv
) and theos.remove()
function to delete a file (delete_me.csv
). We will also handle errors when trying to delete a file that doesn't exist.
Task 2.1: Import the OS module
To start working with the OS module, you first need to import it. Import the OS module in the code cell below.
π Hint
Use the
import
keyword to import the OS module. The code should look like this:import os
π Solution
import os
Task 2.2: Check if a file exists
Use the
os.path.exists()
function to check if a file namedcomma_separated.csv
exists in the current directory.π Hint
Use the
os.path.exists()
function and pass the name of the file as a string. The code should look like this:os.path.exists('filepath.ext')
π Solution
os.path.exists('comma_separated.csv')
Task 2.3: Delete a file
Use the
os.remove()
function to delete a file nameddelete_me.csv
in the current directory. Only delete the file if it exists. If the file exists, printdeleting file!
, and if the file can't be found, printfile missing!
. Run your code twice to print both messages.π Hint
Use the
os.remove()
function and pass the name of the file as a string. Useos.path.exists
in anif-else
block to handle the case that the file doesn't exist.π Solution
# First Run if os.path.exists('delete_me.csv'): print('deleting file!') os.remove('delete_me.csv') else: print('file missing!') # Second Run if os.path.exists('delete_me.csv'): print('deleting file!') os.remove('delete_me.csv') else: print('file missing!')
-
Challenge
Working with CSV Files
Working with CSV Files
To review the concepts covered in this step, please refer to the Read CSVs with Pythonβs CSV Package module of the Reading and Writing CSV Files in Python course.
Knowing how to read and write CSV files is important because CSV is a common data format that is widely used for data storage and transmission. In this step, we will practice reading data from a CSV file and writing data to a CSV file using Python's built-in CSV module.
In this step, we will practice reading and writing CSV files using Python's built-in CSV module. We will use the
csv.reader
,csv.DictReader
andcsv.writer
methods to read and write data from thecomma_separated.csv
file.
Task 3.1: Import the CSV Module
First, we need to import the CSV module to use its functions. Import the CSV module in Python.
π Hint
Use the
import
keyword to import the CSV module.π Solution
import csv
Task 3.2: Open and Read a CSV File
Now, let's open and read a CSV file. Open the
comma_separated.csv
file and read its contents using thecsv.reader
method.π Hint
Use the
open
function to open the file and thecsv.reader
method to read its contents. We'll use awith
statement to ensure the file is properly closed after it is no longer needed. The with syntax looks like:with open(filepath, 'r') as file: #do stuff here
π Solution
with open('comma_separated.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row)
Task 3.3: Read a CSV File as a Dictionary
Sometimes, it's more convenient to read a CSV file as a dictionary. Use the
csv.DictReader
method to read thecomma_separated.csv
file as a list of dictionaries.π Hint
Use the
csv.DictReader
method to read the CSV file as a dictionary. Remember to use awith
statement to ensure the file is properly closed after it is no longer needed.π Solution
with open('comma_separated.csv', 'r') as file: reader = csv.DictReader(file) for row in reader: print(row)
Task 3.4: Write to a CSV File
Finally, let's practice writing to a CSV file. Write the following data to a new CSV file named
output.csv
:['Name', 'Age']
,['Alice', '20']
, and['Bob', '25']
. Use the Jupyter UI to confirm thatoutput.csv
was created with comma separated rows.π Hint
Use the
csv.writer
method to write to a CSV file. Remember to use awith
statement to ensure the file is properly closed after writing. Use thewriterow
method of the writer object to write each row. Open the file in write mode,'w'
.π Solution
with open('output.csv', 'w') as file: writer = csv.writer(file) writer.writerow(['Name', 'Age']) writer.writerow(['Alice', '20']) writer.writerow(['Bob', '25'])
-
Challenge
Importing Data with Pandas and NumPy
Importing Data with Pandas and NumPy
To review the concepts covered in this step, please refer to the Importing with Pandas and NumPy module of the Reading and Writing CSV Files in Python course.
Understanding how to use Pandas and NumPy for data manipulation is important because they provide powerful tools for data analysis and manipulation. In this step, we will practice reading CSV files with Pandas and NumPy, and perform basic data manipulations.
In this step, we will practice using Pandas and NumPy to read CSV files and manipulate data. We will explore how to use Pandas to handle CSV files with different separators, and how to concatenate multiple data frames. There will be a
tab_separated.csv
and a comma separated version,comma_separated.csv
, to read and concat. We will also practice using NumPy'sloadtxt
function to load data from a CSV file.
Task 4.1: Importing Necessary Libraries
Import the necessary libraries for data manipulation, which are pandas and numpy.
π Hint
Use the
import
keyword to import pandas and numpy. Remember to use theas
keyword to give them an alias for easier access.π Solution
import pandas as pd import numpy as np
Task 4.2: Reading CSV Files with Pandas
Read the
comma_separated.csv
andtab_separated.csv
files using pandas and assign them to variablesdf_comma
anddf_tab
respectively. Print the shape of the resulting data frames.π Hint
Use the
pd.read_csv()
function to read the CSV files. Remember to specify the correct separator for each file using thesep
keyword argument. Print the shapes of both data frames using thepd.DataFrame.shape
attribute.π Solution
df_comma = pd.read_csv('comma_separated.csv') df_tab = pd.read_csv('tab_separated.csv', sep='\t') print(df_comma.shape) print(df_tab.shape)
Task 4.3: Concatenating Data Frames
Concatenate
df_comma
anddf_tab
into a single data framedf_concat
. Print the shape of the resulting data frame.π Hint
Use the
pd.concat()
function to concatenate the data frames. Pass the data frames as a list to this function.π Solution
df_concat = pd.concat([df_comma, df_tab]) print(df_concat.shape)
Task 4.4: Loading CSV Files with NumPy
Load the
comma_separated.csv
file using numpy'sloadtxt
function and assign it to the variabledata_loadtxt
. Print the data.π Hint
Use
np.loadtxt()
with thefname='comma_separated.csv'
,dtype=object
,delimiter=','
, andskiprows=1
keyword arguments set. These will specify the correct delimiter and skip the header row, allow for any data type and look for the data in the right location.π Solution
data_loadtxt = np.loadtxt(fname='comma_separated.csv', delimiter=',', skiprows=1, dtype=object) print(data_loadtxt)
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.