Reading CSV Files
Explore how to read CSV files in Python by using from_dict methods and a load function that partitions data. Learn to handle errors like InvalidSampleError for better user feedback and avoid repeating code by validating data with superclass and subclasses.
We'll cover the following...
Using form_dict() method
We’ll provide a common template for creating objects from CSV source data. The idea is to leverage the from_dict() methods of the various classes to create the objects our application uses:
Constructing the load() method
The load() method is partitioning the samples into testing and training subsets. It expects an iterable source of dict[str, str] objects, which are produced by a csv.DictReader object.
The user experience implemented here is to report the first failure and return. This might lead to an error message like the following:
This message has all the required information, but may not be as helpful as desired. We ...