CSV Processing
This is the final chapter of the course, covering one of the most common formats for tabular data β CSV files.
CSV (Comma-Separated Values) is a simple text format where each line is a record and values are separated by a delimiter, usually a comma (but sometimes semicolons, tabs, etc.).
Python's built-in csv
module makes it easy to read, write, and process CSV files without manual string handling.
- Show example code that reads
"people.csv"
using bothcsv.reader
andcsv.DictReader
, than prints rows. - Show example code that writes two additional rows to
"people.csv"
usingcsv.DictWriter
. - Show example code that reads a
people.csv
, modifies some values, and writes the result back with a custom delimiter and quoting.
Reading CSV Files
Open a CSV file and pass it to csv.reader
or csv.DictReader
:
csv.reader
β each row as a list, e.g.['Alice', '30', 'New York']
;csv.DictReader
β each row as a dictionary, using the first row as headers, e.g.{'name': 'Alice', 'age': '30', 'city': 'New York'}
.
DictReader
is often easier since you can access values by column names instead of indexes.
Writing CSV Files
To save data into CSV, use csv.writer
or csv.DictWriter
:
csv.writer
β writes rows as lists with.writerow()
or.writerows()
;csv.DictWriter
β writes rows as dictionaries. Definefieldnames
, call.writeheader()
, then add rows with.writerow()
.
Open files with newline=""
to prevent extra blank lines on some systems.
Working with CSV Data
The csv
module offers options to customize how data is handled:
- Delimiter β change with
delimiter=";"
or"\t"
; - Quoting/Escaping β control with
quotechar='"'
andquoting=csv.QUOTE_ALL
orcsv.QUOTE_MINIMAL
; - Line endings β set
lineterminator="\n"
or"\r\n"
for consistency; - Encoding β use
encoding="utf-8"
(or another) for non-English text; - Large files β read line by line to avoid loading everything into memory.
Summary
- CSV is a universal, human-readable format for tabular data;
- Use
csv.reader
/csv.DictReader
for reading; - Use
csv.writer
/csv.DictWriter
for writing; - Configure delimiters, quoting, and encoding to match the file's structure;
- Handle large files by streaming data instead of loading it all at once.
Try It Yourself
- Create a file named
"people.csv"
with headersname,age,city
and three rows of data; - Read the file using
csv.DictReader
and print each person'sname
andcity
; - Add one more person to the data and write the updated table back using
csv.DictWriter
withfieldnames=['name', 'age', 'city']
; - Re-open the file and verify that the new row appears.
This completes the course. Along the way, we have explored the fundamentals of Python programming.
You learned about basic data types and variables, worked with lists, tuples, sets, and dictionaries, and understood how to organize logic with loops and conditions. We also covered how to build classes and objects, and finally, how to handle files β including text files, JSON, and CSV.
These skills form a solid foundation for solving real-world programming tasks and preparing for more advanced topics. Thank you for studying this course.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 5
CSV Processing
Swipe to show menu
This is the final chapter of the course, covering one of the most common formats for tabular data β CSV files.
CSV (Comma-Separated Values) is a simple text format where each line is a record and values are separated by a delimiter, usually a comma (but sometimes semicolons, tabs, etc.).
Python's built-in csv
module makes it easy to read, write, and process CSV files without manual string handling.
- Show example code that reads
"people.csv"
using bothcsv.reader
andcsv.DictReader
, than prints rows. - Show example code that writes two additional rows to
"people.csv"
usingcsv.DictWriter
. - Show example code that reads a
people.csv
, modifies some values, and writes the result back with a custom delimiter and quoting.
Reading CSV Files
Open a CSV file and pass it to csv.reader
or csv.DictReader
:
csv.reader
β each row as a list, e.g.['Alice', '30', 'New York']
;csv.DictReader
β each row as a dictionary, using the first row as headers, e.g.{'name': 'Alice', 'age': '30', 'city': 'New York'}
.
DictReader
is often easier since you can access values by column names instead of indexes.
Writing CSV Files
To save data into CSV, use csv.writer
or csv.DictWriter
:
csv.writer
β writes rows as lists with.writerow()
or.writerows()
;csv.DictWriter
β writes rows as dictionaries. Definefieldnames
, call.writeheader()
, then add rows with.writerow()
.
Open files with newline=""
to prevent extra blank lines on some systems.
Working with CSV Data
The csv
module offers options to customize how data is handled:
- Delimiter β change with
delimiter=";"
or"\t"
; - Quoting/Escaping β control with
quotechar='"'
andquoting=csv.QUOTE_ALL
orcsv.QUOTE_MINIMAL
; - Line endings β set
lineterminator="\n"
or"\r\n"
for consistency; - Encoding β use
encoding="utf-8"
(or another) for non-English text; - Large files β read line by line to avoid loading everything into memory.
Summary
- CSV is a universal, human-readable format for tabular data;
- Use
csv.reader
/csv.DictReader
for reading; - Use
csv.writer
/csv.DictWriter
for writing; - Configure delimiters, quoting, and encoding to match the file's structure;
- Handle large files by streaming data instead of loading it all at once.
Try It Yourself
- Create a file named
"people.csv"
with headersname,age,city
and three rows of data; - Read the file using
csv.DictReader
and print each person'sname
andcity
; - Add one more person to the data and write the updated table back using
csv.DictWriter
withfieldnames=['name', 'age', 'city']
; - Re-open the file and verify that the new row appears.
This completes the course. Along the way, we have explored the fundamentals of Python programming.
You learned about basic data types and variables, worked with lists, tuples, sets, and dictionaries, and understood how to organize logic with loops and conditions. We also covered how to build classes and objects, and finally, how to handle files β including text files, JSON, and CSV.
These skills form a solid foundation for solving real-world programming tasks and preparing for more advanced topics. Thank you for studying this course.
Thanks for your feedback!