Skip to main content
Programs exist to process data. In Python, you can easily read, manipulate, and save different data formats. We will look at how to handle three of the most common data formats: raw Text, JSON, and CSV, using only Python’s built-in capabilities.

1. Handling Text Data

The simplest format is plain text (.txt). Python provides a built-in open() function to read and write files.

Reading Text Files

Always use the with statement when opening files. It automatically closes the file for you, even if an error occurs.

Writing Text Files

Use "w" to write (overwrites the file) or "a" to append (adds to the end).

2. Handling JSON Data

JSON (JavaScript Object Notation) is the standard data format for web APIs and configuration files. Python has a built-in json module to parse and generate JSON. JSON (JavaScript Object Notation) is a lightweight, text-based format used to exchange data between applications. It is widely used in REST APIs, configuration files, and data storage because it is easy for both humans and machines to read. Python provides the built-in json module to work with JSON data.

JSON Data Types

JSON supports the following data types:

Common JSON Functions

JSON Example


3. Handling CSV Data

CSV (Comma-Separated Values) is the standard format for spreadsheets and tabular data. Python’s built-in csv module makes it easy to read and write CSV files.

Reading CSV Files

You can read rows as lists, or use DictReader to read rows as dictionaries where the keys are the column headers (highly recommended!).

Writing CSV Files

Similarly, you can write rows using lists, or write them using dictionaries with DictWriter.

Practice & Exercises

To reinforce what you’ve learned in this section (reading and writing text files, JSON serialization/deserialization, and CSV list/dictionary readers and writers), practice with these interactive notebooks:

Follow-Along Practice

Practice writing files, reading entire files, looping line-by-line, formatting JSON strings, writing JSON properties to local files, reading CSV rows as lists/dictionaries, and constructing CSV databases using DictWriter.💻 VS Code | 🚀 Colab | 📥 Download

Practice Exercises

Test your knowledge with hands-on exercises on writing uppercase text filters, serializing application user settings, dumping store inventory products, and calculating marks averages from student records.💻 VS Code | 🚀 Colab | 📥 Download

What’s next?

Now that you know how to work with built-in modules and data formats, let’s learn how to download, install, and manage third-party external libraries using PyPI, pip, and uv!

Working with External Modules

Learn to use pip, PyPI, and uv