9.1 Introduction

  • Variables, lists, tuples, dictionaries, sets, arrays, pandas Series and pandas DataFrames offer only temporary data storage
    • lost when a local variable “goes out of scope” or when the program terminates
  • Data maintained in files is persistent
  • Computers store files on secondary storage devices
    • solid-state drives, hard disks and more

9.1 Introduction (cont.)

  • We consider text files in several popular formats
    • plain text
    • JSON (JavaScript Object Notation)
    • CSV (comma-separated values)
  • Use JSON to serialize and deserialize objects for saving to secondary storage and transmitting them over the Internet
  • Intro to Data Science section shows loading and manipulating CSV data with
    • Python Standard Library’s csv module
    • pandas Library

9.1 Introduction (cont.)

  • Python security—we’ll discuss the security vulnerabilities of serializing and deserializing data with the Python Standard Library’s pickle module
    • We recommend JSON serialization in preference to pickle
  • exception handling
    • An exception indicates an execution-time problem
    • E.g., ZeroDivisionError, NameError, ValueError, StatisticsError, TypeError, IndexError, KeyError and RuntimeError
    • Deal with exceptions as they occur by using try statements and except clauses
    • Help you write robust, fault-tolerant programs that can deal with problems and continue executing or terminate gracefully

9.1 Introduction (cont.)

  • Programs typically request and release resources (such as files) during program execution
  • Often, these are in limited supply or can be used only by one program at a time
  • You'll see how to guarantee that after a program uses a resource, it’s released for use by other programs, even if an exception has occurred
  • Use the with statement for this purpose

©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.

DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.