Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Mapping CSV Rows to Dictionaries

CSV files are an efficient and very common way to move data around. The can be imported into Excel very easily for review and editing, and then exported to CSV for other operations. They can also easily be imported into pandas dataframes for work. So in this lab we will use Python's `csv` module to read CSV into dictionaries, allow the intended user to make necessary operations on the data in the dictionaries, and then return them to CSV for further operations. You will need to know basic Python programming and have knowledge of Python's `csv` module: - [Certified Associate in Python Programming Certification](https://linuxacademy.com/cp/modules/view/id/470)

Google Cloud Platform icon
Lab platform
Lab Info
Level
Beginner
Last updated
Sep 09, 2025
Duration
30m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Convert a CSV File to a List of Dictionaries

    The LMS provides a CSV file for the test class named student_data.csv. You have a skeleton app that will let you write code to test the ability to make the CSV data into a dictionary.

    Before writing code, please run the file to see what is failing:

    python csv_dict_conversion.py
    

    You will get a message that "the returned data does not match the expected dict." So please complete the function def csv2dict(filename) in csv_dict_conversion.py. The filename will be provided in the main function of the file.

    def csv2dict(filename):
    
        # open file and create DictReader object
        # use the DictReader to get data and
        # add data to a variable named data_list
        with open(filename, newline='') as infile:
            reader = csv.DictReader(infile)
    
            data = []
            for row in reader:
                data.append(row)
            return data
    

    Run python csv_dict_conversion.py.

    Congratulations! The data is in a format the teacher's app can use.

  2. Challenge

    Convert a List of Dictionaries to CSV

    The main function simulates that the teacher has added the grades.

    Please run python csv_dict_conversion.py. You should see an error that "the written data does not match the expected data." Please complete the function def dict2csv(filename, fieldnames, data). The filename and fieldnames are provided for you. The data is the dictionary data created in the last objective with the grades added. This is also provided for you.

    def dict2csv(filename, fieldnames, data):
        # open file and create DictWriter object
        # use the DictWriter to write csv data
        # to file
    
        with open(filename, 'w', newline='') as outfile:
            writer = csv.DictWriter(outfile, fieldnames=fieldnames)
            writer.writeheader()
            writer.writerows(data)
    
    Run `python csv_dict_conversion.py`.
    
    **Awesome!** You have mastered moving data from CSV to dictionaries and dictionaries to CSV.
About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight