CSV Files

//CSV Files
CSV Files 2016-12-12T14:29:58+00:00

Creating and using CSV Files

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…

Create a CSV string for use in CSV files

Joining variables together to form a CSV string

LitDev & CSV Files

The LitDev extension includes several methods in the LDFile object that work with or make CSV file structures.  These are useful for bulk actions but limited if you need to be able to extract individual values unless you are prepared to master two dimensional arrays.

These are not generally required for UK GCSE courses so I do not intend to cover it at this stage.  However, if you are really interested in coding then it is very worthwhile mastering these and three dimensional arrays.

In Small Basic a two dimension array looks like: ArrayName[dimension 1][dimension 2] and would appear in your code as Variable[1][1], Variable[1][2], etc.  If you thing of a grid, the first index could represent the rows and the second index represent to columns.

Breaking them apart requires little more effort.  An algorithm to tackle the problem is shown on the right which translates into the following example code.

Splitting a string from a CSV file

Code to split a CSV string into its component parts

The subroutine shown expects a variable called ‘Data’ to be available and contain the required CSV text.  This is then split into an array called return.

Split a string from a CSV file v2

This versions uses a LitDev call to a function to return the components of your CSV string

This is very similar to the standard SB code except that you pass it the CSV text as a parameter which simplifies the coding but does  not change the coding much.

End of Topic Tasks

CSV Algorithm

  1. With a line of CSV values
  2. Find the position of the first comma in the data
  3. If you found a comma,
    1. Extract the left part of the data up to (but not including) the comma and store it in your variable
    2. Extract the right part of the data up to (but not including) the comma and store it in your original line of values
    3. Go back to step 2 and do the same again
  4. If there is no comma, you have the last item of data in the line.