Read and extract value from csv file

Hello,
Need help to extract values from csv file as nc(j,k) as shown in this vb6 code :

’ open the text file that contains the data
Dim fs As FileSystemObject ’ ******* need to reference Microsoft Scripting Runtime
Dim coefficientsFile As TextStream
Dim textFileData() As String

Set fs = New FileSystemObject
Set coefficientsFile = fs.OpenTextFile("C:\NavFolder\NutCoef.csv", ForReading)

'----- Read the coefficients from the data statements
'----- only if not already done.
If (NutTermsRead = 0) Then
    ReDim nc(1 To 106, 1 To 9) As Double
           
    For j = 1 To 106
        textFileData = Split(coefficientsFile.ReadLine, ",")
        For k = 1 To 9
            nc(j, k) = CDbl(textFileData(k - 1))
         'Debug.Print " nc() " & nc(j, k)
        Next k
    Next j
    NutTermsRead = 1


End If

coefficientsFile.Close
Set coefficientsFile = Nothing
Set fs = Nothing

I use the Chilkat CSV plugin, which is available for free (some of the Chilkat classes require a license but CSV isn’t one of them).

var csv as New Chilkat.CSV, NC(105, 8) as Double
CSV.HasColumnNames = False
var success as boolean = CSV.LoadFile("C:\NavFolder\NutCoef.csv")
if Success then

for j as integer = 0 to 105
for k as integer = 0 to 8
NC(j, k) = CSV.GetCell(j, k).ToDouble
next k
next j
else
MessageBox "Could not open CSV File."
end if

Awsome, running properly lot of features in chiklat !
Very useful …
Thank you very much Christian Wheel

Another upvote for the Chilkat CSV plugin / CSV parser.

I’m working with a CSV file with the following characteristics:

  • 1500 records with 20 fields per record
  • each record contains 3 fairly big/complicated text fields
  • CSV file is about 5.8 MB in size

My initial testing has been parsing out one field out of each record. The original parser I used was taking about 30 seconds, the Chilkat parser took it down to less than 1 second total.

2 Likes