• Labs icon Lab
  • Data
Labs

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.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 41m
Published
Clock icon Dec 06, 2023

Contact sales

By filling out this form and clicking submit, you acknowledge ourΒ privacy policy.

Table of Contents

  1. 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 the read method, and write new data using the write 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 using readlines and a for loop. We'll be working with the example_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 the print 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 the write 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 a for 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()
    
  2. 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 the os.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 named comma_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 named delete_me.csv in the current directory. Only delete the file if it exists. If the file exists, print deleting file!, and if the file can't be found, print file 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. Use os.path.exists in an if-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!')
    
  3. 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 and csv.writer methods to read and write data from the comma_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 the csv.reader method.

    πŸ” Hint

    Use the open function to open the file and the csv.reader method to read its contents. We'll use a with 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 the comma_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 a with 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 that output.csv was created with comma separated rows.

    πŸ” Hint

    Use the csv.writer method to write to a CSV file. Remember to use a with statement to ensure the file is properly closed after writing. Use the writerow 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'])
    
  4. 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's loadtxt 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 the as 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 and tab_separated.csv files using pandas and assign them to variables df_comma and df_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 the sep keyword argument. Print the shapes of both data frames using the pd.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 and df_tab into a single data frame df_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's loadtxt function and assign it to the variable data_loadtxt. Print the data.

    πŸ” Hint

    Use np.loadtxt() with the fname='comma_separated.csv', dtype=object, delimiter=',', and skiprows=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.