Working with CSV
A nice and simple way to store uncomplicated and small datasets is CSV. This format is also of interest if you're working with spreadsheet applications such as Microsoft Excel, as they have excellent support for importing and exporting various flavors of CSV.
Getting started
You probably already know what a CSV is, but a little refresher won't hurt.
The idea of the format is to take a table of values and write all rows down as records. Inside a record, every column item is written down and separated by a comma. That's where the format's name comes from—comma-separated values.
Let's do an example. In the following code, we are going to write a CSV comparing various planets in the solar system to our own. A radius, distance from the sun, and gravity of 1
means exactly as on earth. Written as a table, our values look like this:
name | radius | distance_from_sun | gravity |
Mercury | 0.38 | 0.47 | 0.38 |
Venus | 0.95 | 0.73 | 0.9 |
Earth | 1 | 1 | 1 |
Mars | 0.53 | 1.67 | 0.38 |
Jupiter | 11.21 | 5.46 | 2.53 |
Saturn | 9.45 | 10.12 | 1.07 |
Uranus | 4.01 | 20.11 | 0.89 |
Neptune... |