Ravindra BagaleCourses & study guides मराठी Track your progress

How to Read and Write CSV, JSON and Text Files in Python (with open and with)

Ravindra Bagale's Tip — Chala mitrano

Data lives in files: CSV exports, JSON from apps, plain text logs. Today we read and write all three with plain Python, so you know what pandas read_csv does for you.

What you will learn in this chapter

  • File paths and the working folder
  • Read and write text files with open() and with
  • Read a CSV with the csv module
  • Write a CSV
  • Read and write JSON
  • Common file errors and encoding

Practice data

All names, shops, banks, hospitals and amounts in this chapter are made up for learning. They are not real people or real company numbers.

Concepts in this chapter

  1. P9.1File Paths and the Working Folder in Python
  2. P9.2How to Read and Write Text Files in Python
  3. P9.3How to Read a CSV File in Python with the csv Module
  4. P9.4How to Write a CSV File in Python
  5. P9.5How to Read and Write JSON Files in Python
  6. P9.6Common File Errors in Python and How to Fix Them