<< Click to Display Table of Contents >> Navigation: ThinBASIC Modules > CSV > cCSV class |
The functionality is exposed via cCSV class, offering all methods and properties for CSV interaction.
How to use this module
Example Script
'---Script created on 03-10-2023 17:36:25 by Eros Olmi
uses "CSV"
uses "console"
Dim myTimer As New cTimer
dim nRec as Long
dim nCol as long
printl "CSV Creation ..."
dim CSV_Sample_001 as new cCSV
'---Delimiter can be changed with .char.delim property (read/write) or in .Load method
' Default delim char is comma ,
'CSV_Sample_001.char.Delim = ";"
'---CSV can have remarks line starting with # char
' Remark char can be changed with the .char.remark property
'CSV_Sample_001.char.remark = "$"
'---Start timer
MyTimer.Start
'---Load CSV_Sample_001 data. Load method params:
' file name (mandatory)
' [true/false if first line is columns names (optional)
' [char seperator] (optional)
printl "Loading CSV file"
printl "Rows buffer default:", CSV_Sample_001.Row.Buffer
'printl CSV_Sample_001.Load(APP_ScriptPath + "Sample-Spreadsheet-5000-rows.csv", false, ",")
printl CSV_Sample_001.Load(APP_ScriptPath + "Sample-Spreadsheet-10000-rows.csv", true, ",")
printl "File loaded....:", CSV_Sample_001.FileName
printl "Char Separator.:", CSV_Sample_001.Char.Delim
printl "# of columns...:", CSV_Sample_001.Column.Count '---Number of columns
printl "# of rows......:", CSV_Sample_001.Row.Count '---Number of rows
printl
'---If CSV does not have columns names in first line, by default all columns names will be named "c" folowed by a number
' so c1, c2, c3, ...
printl "Name of columns"
for nCol = 1 to CSV_Sample_001.Column.Count
printl nCol, ":",
CSV_Sample_001.Column.Name(nCol), '---Get name giving ordinal
CSV_Sample_001.Column.Index(CSV_Sample_001.Column.Name(nCol)) '---Get ordinal giving name
Next
PrintL
'---Columns name is a get/set property. Here we change the last one and print out again
printl "Name of columns (changed last one into LastColumn)"
CSV_Sample_001.Column.Name(CSV_Sample_001.Column.Count) = "LastColumn"
for nCol = 1 to CSV_Sample_001.Columns
printl nCol, ":",
CSV_Sample_001.Column.Name(nCol),
CSV_Sample_001.Column.Index(CSV_Sample_001.Column.Name(nCol))
Next
PrintL
PrintL
printl "Elapsed loading time (sec):", MyTimer.Elapsed(%cTimer_Seconds)
printl
printl "Press a key to show first 5 lines of data data data"
WaitKey
for nRec = 1 to min(5, CSV_Sample_001.Rows)
for nCol = 1 to CSV_Sample_001.Columns
printl "Row:", nRec, "Col:", nCol, "Data:", CSV_Sample_001.Data(nRec, nCol), ""
next
printl
next
printl "---Closing ... press a key to end---"
WaitKey