liveBook · Manning

Text

As explained in The Quick Python Book, Fourth Edition

Text is written language represented as a sequence of characters, including content stored in plain-text files.

Text is written language represented as characters, commonly stored in plain-text files. It may be unstructured, such as tweets, literary works, or news stories, or structured as rows whose fields use delimiters such as commas, tabs, or pipes. section 21.0

Reading text requires attention to encoding. ASCII covers 128 characters but cannot represent many accented letters, symbols, or writing systems. UTF-8 preserves ASCII characters while supporting the much larger Unicode character set, making it a practical default when the file’s encoding is uncertain. section 21.2.1

Process text by making its logical units and cleanup rules explicit:

  • Choose whether a unit is a line, paragraph, tweet, news story, or another application-specific segment. section 21.2.2
  • Split paragraphs on the file’s separator, such as two newline characters when blank lines mark paragraph boundaries. section 21.2.2
  • Normalize before word-level processing by changing case, removing selected punctuation, and splitting on whitespace. section 21.2.2
  • Handle undecodable bytes deliberately with policies such as ignore, replace, surrogateescape, or backslashreplace. section 21.2.1

Turning a novel into paragraphs and words

Read the file as one string with `open("moby_01.txt").read()`, then use `moby_text.split("\n\n")` to produce paragraphs. For word processing, lowercase one paragraph, remove periods and commas, and call `split()` to produce a word list. section 21.2.2

open('test.txt', errors='ignore').read()
 
'ABC'
 
open('test.txt', errors='replace').read()
 
'ABC   '                                       #1
 
open('test.txt', errors='surrogateescape').read()
 
'ABC\udcff\udcc0\udcc1'
 
open('test.txt', errors='backslashreplace').read()
 
'ABC\\xff\\xc0\\xc1'
#1 Replaced error characters with chr(65533)

Compare decoding-error policies on bytes that are invalid under UTF-8: ignoring drops them, replacing marks them, and alternative policies preserve their original values or escapes. section 21.2.1

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains encoding, segmentation, normalization, and error handling, but stops short of comprehensive text-processing methods.
Examples
Uses tweets, Moby Dick, news stories, paragraph splitting, word extraction, and invalid-byte decoding examples.
What it assumes
Assumes general programming knowledge while teaching encoding choices and logical-unit selection through concrete file examples.
What you can do after
Apply it via Python to read text, choose units, split paragraphs, normalize words, and manage decoding errors.
How it connects
Links text with text encoding, ASCII, Unicode, UTF-8, unstructured text, and structured data files.

Also explained in

Compare how 3 books explain text →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.