Your Cart
Loading

Code Clinic - The Build - Book 3: Reading Files

On Sale
$12.00
$12.00
Added to cart

Real data never arrives typed into your script. It arrives as files, thousands of them, and this book gets you reading them. It begins with what a file actually is, then opens one with open() and pulls its contents in with read(). You learn why every file you open has to be closed, what goes wrong when it is not, and how the with statement closes it for you even if your code fails halfway through. From there the book separates two ways of seeing the same file: one long string, or a list of lines. readlines() gives you the list. Looping straight over the file object gives you the lines one at a time, without pulling everything into memory first. The last chapter deals with the invisible problem that catches out everyone new to text files, the newline character stuck to the end of every line, and how strip() removes it along with stray spaces so the line is clean enough to parse. The working example is a real .hl7 file read off disk.


Nineteen A4 pages, in full colour. Written in short sentences and plain English, for readers whose first language is not English and clearer for everyone else because of it.


Every code example in this book was run before the page was printed, so the output you read is the output you get. Instant PDF download. By M.R. Sterling.


You will get a PDF (864KB) file