Text files aren’t the only thing that Python can read, but they’re the only thing that we don’t need any additional parsing library to understand. CSV files are an example of a text file that impose a structure to their data. CSV stands for Comma-Separated Values and CSV files are usually the way that data from spreadsheet software (like Microsoft Excel or Google Sheets) is exported into a portable format. A spreadsheet that looks like the following
In a CSV file that same exact data would be rendered like this:
Name,Username,Email Roger Smith,rsmith,email@example.com Michelle Beck,mlbeck,firstname.lastname@example.org Ashley Barker,a_bark_x,email@example.com Lynn Gonzales,goodmanjames,firstname.lastname@example.org Jennifer Chase,chasej,email@example.com Charles Hoover,choover,firstname.lastname@example.org Adrian Evans,adevans,email@example.com Susan Walter,susan82,firstname.lastname@example.org Stephanie King,stephanieking,email@example.com Erika Miller,jessica32,firstname.lastname@example.org
Notice that the first row of the CSV file doesn’t actually represent any data, just the labels of the data that’s present in the rest of the file. The rest of the rows of the file are the same as the rows in the spreadsheet software, just instead of being separated into different cells they’re separated by… well I suppose it’s fair to say they’re separated by commas.
CSV files are just plain text files!
logger.csv using our standard
with syntax, saving the file object in the temporary variable
Print out the contents of
logger.csv by calling
.read() on the file. Notice that it is parsed as a string.