So far, all (or most of) your files have contained a single item of data on each line. Although this is perfectly acceptable, you frequently need to store the data as lines of several items of data. The final task in the last chapter could, however, best be solved if each line in your file contained multiple values. I.e. you could store not only the name of the planet but also its size, distance from the Sun, astronomical year and any other items of data for each planet. To do this we use CSV files – Comma Separated Values files. An added bonus using CSV files it that they can be opened directly in most spreadsheets.
In this type of text file, each value on a line is separated by – you guessed it – a comma. In truth, any unique character could be used as long as it does not appear within the normally expected data values. If it did it would confuse. A frequently used alternative character is the TAB character (UTF-8 code 9).
Creating a line of data to save in this format is simple – you just have to join the variables together with the required character…
Joining variables together to form a CSV string