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 3.88 30.33 1.14

 

Take every row, separate the values by commas, put them each on a separate line, and you end up with the CSV file:

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,3.88,30.33,1.14

As you can see, the heading ( planet,radius,distance_from_sun,gravity) is simply written as the first record.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.47.166