liveBook · Manning

Chapter 21: Processing data files

What you will learn in chapter 21 of The Quick Python Book, Fourth Edition

The introduction explains why processing text files matters: data may be unstructured or delimited into records, and datasets may be large and dirty.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why processing text files matters: data may be unstructured or delimited into records, and datasets may be large and dirty.

Key terms: unstructured text, record, fields, delimited, dirty

21.1 Welcome to ETL

Introduces extract-transform-load (ETL) as a way to describe getting data from files into a useful form and storing the result.

Key terms: extract-transform-load (ETL), extraction, transformation, loading

21.2 Reading text files

Introduces file reading as the extract step of ETL and explains why large files may need to be processed in smaller pieces.

Key terms: ETL, extract, memory, smaller segments, one line at a time

21.2.1 Text encoding: ASCII, Unicode, and others

This section explains why text-file encodings can cause reading errors, why UTF-8 is a useful default, and how Python can handle invalid characters.

Key terms: ASCII encoding, 7-bit values, 8-bit bytes, Unicode, UTF-8, ASCII characters, open, errors, strict, ignore

21.2.2 Unstructured text

This section shows how to choose useful units in unstructured text and normalize words before counting them.

Key terms: unstructured text, basic logical unit, line, paragraph, split(), newline, normalization, lower, replace, translate

21.2.3 Delimited flat files

This subsection explains how delimiters give text files fields that Python can split into values, while leaving type conversion to a later step.

Key terms: delimited, field, pipe, comma-separated values (CSV), split(), list of values, strings

21.2.4 The csv module

This section shows why Python’s csv module is preferable to hand-written splitting when reading delimited files, especially files with quoted fields.

Key terms: csv module, delimited files, trailing newline character, quoted fields, delimiter

21.2.5 Reading a csv file as a list of dictionaries

This subsection shows how csv.DictReader returns rows keyed by field name and explains when that convenience is worth its reading cost.

Key terms: DictReader, field name, dictionary, specific fields

21.3 Excel files

This section explains how to extract data from a simple Excel spreadsheet and when CSV is a better format for processing data.

Key terms: Excel files, CSV files, delimited files, standard library, openpyxl, workbook, sheet, rows, cells, formatting

21.4 Data cleaning

This section defines dirty data in text-based files and names the process of dealing with it data cleaning.

Key terms: dirty data, data cleaning, null values, encoding, extra whitespace, unsorted

21.4.1 Cleaning

This subsection shows how missing-value markers and formatted numbers in data files can interfere with calculations, and describes two ways to handle missing values.

Key terms: 'Missing', heat index, % Coverage, strings, None, empty string

21.4.2 Sorting

This subsection explains why sorting data files helps processing and how to choose a sorting approach based on memory, sort requirements, and performance.

Key terms: duplicate values, related rows, item ID, sort method, sorted() function, available memory, UNIX sort command, key function, lambda function, case insensitive

21.4.3 Data cleaning problems and pitfalls

This subsection identifies common hidden causes of data-loading problems and suggests ways to isolate errors during cleaning.

Key terms: whitespace, null characters, punctuation, CSV files, exceptions, print statements, logging, Python debugger

21.5 Writing data files

This section explains why transformed data is often written to files and what an output file specification typically defines.

Key terms: ETL, transformed data, file specification, fields, format, constraints

21.5.1 CSV and other delimited files

This subsection explains how to write cleaned data to delimited files with the csv module, including how to write dictionary rows with DictWriter.

Key terms: csv module, delimiter, delimited files, DictWriter, field names, writeheader

21.5.2 Writing Excel files

This subsection outlines how to create an Excel spreadsheet and notes that cells can be formatted while writing it.

Key terms: workbook, sheet, cells, CSV data file, formatting

21.5.3 Packaging data files

This subsection explains when to package related or large data files in a compressed archive and why a zip file is a practical choice.

Key terms: compressed archive, zip file

21.6 Weather observations

Sets a weather-file processing exercise: extract Cook County observations, clean selected values, and verify the resulting table.

Key terms: Cook County, CSV, Missing, percentage, ---

21.6.1 Solving the problem with AI-generated code

This subsection explains why the weather-observations problem is a reasonable candidate for AI-generated code and what its prompt needs.

Key terms: procedural, higher-level design, AI solution, prompt

21.6.2 Solutions and discussion

21.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 20: Basic file wrangling Chapter 22: Data over the network →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.