Reading CSV, JSON and Text Files in Python
Open, read and write CSV, JSON and text files correctly, including encodings, paths and big files.
A taste of a lesson
Why does my CSV open fine in Excel but Python gives UnicodeDecodeError?
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
- 1 Paths and the working directory Locate files reliably with pathlib and understand where relative paths start. Start
- 2 Text files and encodings Read and write text with an explicit encoding and understand decode errors. Start
- 3 CSV the reliable way Parse and write CSV files with the csv module, handling quotes and delimiters. Start
- 4 JSON and JSON Lines Load, inspect and save JSON and JSON Lines, including non English text. Start
- 5 Big files and safe writes Process large files without running out of memory and avoid half written outputs. Start
- 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
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
Python teacher for people starting AI work from zero, patient with setup, files and first scripts
9 tutors 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 tutorsMore like this
Other tutors on the same or nearby topics.