Following on from the work on plain text files, it is also important to understand how Python can be used with CSV files.
A file with the CSV file extension is a Comma Separated Values file. All CSV files are plain text, can contain numbers and letters only, and structure the data contained within them in a tabular, or table, form.
Files of this format are generally used to exchange data, usually when there's a large amount, between different applications. Database programs, analytical software, and other applications that store massive amounts of information (like contacts and customer data), will usually support the CSV format.
A Comma Separated Values file might sometimes be referred to as a Character Separated Values or Comma Delimited file but regardless of how someone says it, they're talking about the same CSV format.
The image below shows a typical CSV file, in this case showing American states with their representative bird. Note the use of column Headings.
Before we can begin working in Python, we need to make sure we have a file to work with. Download the file below and save it in a folder.
Next, create the program below and run it - you will need to make sure your Python program is saved to the SAME folder as the CSV file!
Each row is read as a list of strings representing the fields in the row.
We're going to do some processing of real-world data now, using freely available airline data sets from the OpenFlights project.
We can see from the data file that "Name" is the second field in each row of data. This means in the list of fields it will have index 1 (index 0 is the first field.)
Here's some code that prints the name of every airport:
I bet the output is still running!! Remember we are now working with large data files - 12000 rows!
Let's expand out code so that it only outputs airports from specific countries.
You can use this code to work with basic data output of specific rows and fields.
One of the most common tasks when working with CSV files is searching for information and outputting related data. In this section we are going to carry out a simple search.
Use the code below to search for the product number 2999.
Once you have tested it works, play around with the number of entries in the CSV file and search different fields to return different values.
It is essential to be able to write information back to a CSV file. The example code below gives you the outline of how this can be achieved for a single line and multiple lines of data. This code can be adapted to fit your needs.
Single line of data:
Multiple lines of data:
Thanks [name] from [whichclass][location], Mr McG will get your message…"
[message]"…and will get back to you ASAP via [email]. Catch you soon.
Have a great day!!
K McGuinness - 2018
No personal data will be shared with any third party. The only data processed via this website is anonymous browsing data to best manage your user experience.