• Labs icon Lab
  • Data
Labs

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.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 33m
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

    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 the name 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 the mode 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 the closed 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 the readline() 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 the readlines() 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)
    
  2. 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.

    1. Open the CSV file using with open('filename.csv', 'r') as file:.
    2. Read the file using csv.reader(file).
    3. Fetch the first row with next(csv_reader) for headers.
    4. Increment a counter in a loop to count rows.
    5. Access elements of the first row for column data.
    6. 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.
    
  3. 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 and as 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 the np.genfromtxt() function. Print the data after it has been loaded. Notice the difference from Task 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 the np.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 the np.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)
    
  4. 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 and as 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 the parse_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.