Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn CSV Processing | File I/O & Data Handling
Introduction to Python with AI

bookCSV 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.

Note
Note
  • Show example code that reads "people.csv" using both csv.reader and csv.DictReader, than prints rows.
  • Show example code that writes two additional rows to "people.csv" using csv.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. Define fieldnames, 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='"' and quoting=csv.QUOTE_ALL or csv.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

  1. Create a file named "people.csv" with headers name,age,city and three rows of data;
  2. Read the file using csv.DictReader and print each person's name and city;
  3. Add one more person to the data and write the updated table back using csv.DictWriter with fieldnames=['name', 'age', 'city'];
  4. 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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 5

bookCSV 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.

Note
Note
  • Show example code that reads "people.csv" using both csv.reader and csv.DictReader, than prints rows.
  • Show example code that writes two additional rows to "people.csv" using csv.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. Define fieldnames, 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='"' and quoting=csv.QUOTE_ALL or csv.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

  1. Create a file named "people.csv" with headers name,age,city and three rows of data;
  2. Read the file using csv.DictReader and print each person's name and city;
  3. Add one more person to the data and write the updated table back using csv.DictWriter with fieldnames=['name', 'age', 'city'];
  4. 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.

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 3
some-alt