Skip to content
SamplePreview build: teacher profiles, ratings, reviews and lesson counts are sample data.
All tutors

Reading CSV, JSON and Text Files in Python

Open, read and write CSV, JSON and text files correctly, including encodings, paths and big files.

Your first 3 replies from the tutor are free. Then you can continue the lesson for $3 from your credit.

A taste of a lesson

One question and the answer, as this tutor gives it.

Reading CSV, JSON and Text Files in Python AI tutor following Efua Mensah's plan
Student:

Why does my CSV open fine in Excel but Python gives UnicodeDecodeError?

Tutor:

Because Python is being strict and the spreadsheet is guessing. Your file was probably saved in an older Windows encoding such as cp1252, while Python is reading it as UTF-8, so a character like an accented e becomes an invalid byte sequence. Try open(path, encoding='cp1252'), or if the file starts with an odd invisible character, encoding='utf-8-sig'. Avoid errors='ignore', which silently deletes characters. Do you know which tool or system exported this file?

Written by the teacher as an example. In your lesson the tutor answers your own questions, and like any AI it can be wrong.

What you will be able to do

  • Read and write text, CSV, JSON and JSON Lines files safely
  • Handle encodings and fix UnicodeDecodeError without losing data
  • Build file paths that work on any operating system
  • Process files too large to load into memory at once

Lesson plan

6 lessons. Pick one to start there.

  1. 1 Paths and the working directory Locate files reliably with pathlib and understand where relative paths start. Start
  2. 2 Text files and encodings Read and write text with an explicit encoding and understand decode errors. Start
  3. 3 CSV the reliable way Parse and write CSV files with the csv module, handling quotes and delimiters. Start
  4. 4 JSON and JSON Lines Load, inspect and save JSON and JSON Lines, including non English text. Start
  5. 5 Big files and safe writes Process large files without running out of memory and avoid half written outputs. Start
  6. 6 Clean a real export end to end Turn an awkward export into clean UTF-8 JSON Lines ready for an AI pipeline. Start

Try asking

Tap a question to start a lesson with it.

About this tutor

For beginners who need to get data in and out of Python before anything AI related can happen. You practise reading and writing plain text, CSV, JSON and JSON Lines, using paths that work on any operating system, choosing the right encoding, and handling the errors real files throw at you: a stray byte, a missing column, a file too big to load at once. The tutor uses the standard library first so you see what is happening, then shows where pandas makes life easier. Useful before working with datasets, prompt files or model outputs saved to disk.

Reviews

Students can review a tutor after a paid lesson. Nobody has yet.

About the teacher

Efua Mensah

Python teacher for people starting AI work from zero, patient with setup, files and first scripts

9 tutors 4.5(20) 377 lessons taught Sample

I teach Python to people who want to build with AI but have never written much code, or who tried once and got stuck on setup. My background is in teaching adult evening coding classes and later writing data scripts for small research teams, so I know where beginners lose an afternoon: the wrong interpreter, a confusing traceback, a file...

See Efua's profile and tutors