0% found this document useful (0 votes)
16 views

Python Pages Doc Ic Ac Uk CPP Lessons CPP 10 Files 09 Csvreaddict HTML

This document is a chapter from a book about Python programming for C++ programmers. It focuses on Chapter 10 which covers working with different file types in Python, including text files, JSON files, CSV files, and pickle files. Specifically, it discusses how to read CSV files into a dictionary in Python using the csv.DictReader object, which allows accessing column values using names as keys rather than indices. It provides an example of reading student data from a CSV file into a dictionary and printing selected values.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python Pages Doc Ic Ac Uk CPP Lessons CPP 10 Files 09 Csvreaddict HTML

This document is a chapter from a book about Python programming for C++ programmers. It focuses on Chapter 10 which covers working with different file types in Python, including text files, JSON files, CSV files, and pickle files. Specifically, it discusses how to read CSV files into a dictionary in Python using the csv.DictReader object, which allows accessing column values using names as keys rather than indices. It provides an example of reading student data from a CSV file into a dictionary and printing selected values.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python Programming home

Department of Computing, Imperial College London

Python for C++ Programmers


Chapter 1: Introduction
Chapter 2: Basic data types
Chapter 3: Variables and operators
Chapter 4: Sequence types
Chapter 5: Sets and dictionaries
Chapter 6: Control flow
Chapter 7: Functions
Chapter 8: Object-oriented programming
Chapter 9: Modules
Chapter 10: Files
[10.1] Handling text files
[10.2] JSON files
[10.3] Loading JSON files
[10.4] Writing to JSON files
[10.5] pickle
[10.6] Pickling time!
[10.7] CSV files
[10.8] Reading CSV files
[10.9] Reading CSV files into a dict
[10.10] Writing to CSV files
[10.11] That's a wrap!

Chapter 10: Files


>> Reading CSV files into a dict
face Josiah Wang

It might be a hassle to count the index of columns you want from a CSV, especially when there are too many
columns!
To make life easier, you can also read in the CSV files into a dict, using a csv.DictReader object. You can then
access elements using the column names as keys (from the first row). There is also no need to explicitly read the
header row (this is automatically done by csv.DictReader).

1 import csv
2
3 with open("students.csv") as csv_file:
4 reader = csv.DictReader(csv_file)
5
6 for row in reader:
7 print(f"Student {row['name']} is from the Faculty of {row['faculty']}, "
8 f"{row['department']} dept.")

If the CSV file does not contain a header row, you will need to specify your own keys. You can do this by
assigning the fieldnames argument with a list that contains your column names.
1 headers = ["name", "faculty", "department"]
2 reader = csv.DictReader(csv_file, fieldnames=headers)

Previous Next 

Page designed by Josiah Wang Department of Computing | Imperial College London

You might also like