What is CSV?

Comma separated values (CSV) is a file format that is used to exchange data between programs. It uses commas to separate values and requires equally many values on each line.


For a game, we want to save the best player’s score, its name and the date on which the score was achieved. The format we choose therefore is: [player’s name], [score], [date]


This means that Bob scored 12 points at 2013-01-02. We can easily store multiple scores by writing them one below another.


Observation:  CSV files can hold one to many records or lines with values.


We have a sensor that saves the measured temperature in a file. The first value of a line describes the hour, the second the minute, and the third the temperature. The format is: [hour], [minute], [temperature]


For instance, the first line means that at 7:38 a temperature of 12 were measured.

Observation: It can be difficult to understand a CSV file if you don’t know what every value stands for – what the format is.


Read CSV

To load a CSV file, a while-loop reads line for line. Within the loop the subroutine ExtractValues() is called to extract all values from the line. After the loop is finished, all values are written to TextWindow by ShowValues().

filename = "data.csv"
line_number = 1
'   reads line for line, saves values to csv and shows them
line = File.ReadLine(filename, line_number)
While line <> ""
  line_number = line_number + 1
  line = File.ReadLine(filename, line_number)
'   ShowValues - writes all values of csv to the TextWindow
Sub ShowValues
  For line_number = 1 to Array.GetItemCount(csv)   
    For value_number = 1 to Array.GetItemCount(csv[line_number])
      TextWindow.Write(csv[line_number][value_number] + " ")

The ExtractValues() subroutine works on the current line. It extracts all values and saves them into the CSV array at the according position.

'   ExtractValues - saves all values of the current line to csv
Sub ExtractValues 
  'repeat as long as there is a comma
  While Text.GetIndexOf(line,",") > 0
    comma_position = Text.GetIndexOf(line, ",")   
    'save text until first comma
    values[Array.GetItemCount(values)+1] = Text.GetSubText(line, 1, comma_position-1)
    'remove saved text and comma from the line
    line = Text.GetSubTextToEnd(line, comma_position+1)
  'last value does not have a comma at the end
  values[Array.GetItemCount(values)+1] = line
  'append read values to csv
  csv[Array.GetItemCount(csv)+1] = values  
  'reset variable
  values = ""

Finally, all values of the CSV file can be accessed by the CSV array. For instance, you can get the third value of the second line by: csv[2][3]

Write CSV

To save an array as a CSV file, it has to be processed row for row. All values of one row are combined to one text whereby commas are inserted between the values. This text is then written to the CSV file.

csv = "1=1\=1\;2\=2\;3\=3\;;2=1\=4\;2\=5\;3\=6\;;3=1\=7\;2\=8\;3\=9\;;"
csv_filename = "data.csv"
For line_number = 1 to Array.GetItemCount(csv)
  line = csv[line_number][1]
  For field_nr = 2 to Array.GetItemCount(csv[line_number])
    line = line +  "," + csv[line_number][field_nr]
  ' write line to file
  File.AppendContents(csv_filename, line)


Other Languages