Hello,
Read file text and extract values in lines comma delimiter.
This code load values in listbox cell, i need to extract each value delimited by comma, as shown (column) in the image attached to this post.
[code] Dim f As FolderItem
Dim textInput As TextInputStream
Dim rowFromFile, oneCell As String
Dim i As Integer
f = GetOpenFolderItem(“text/plain”) // defini comme FileType
If f <> Nil And f.Exists Then
Dim comma As String = ChrB(44)
textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.WindowsANSI
While Not textInput.EOF
rowFromFile = textInput.ReadLine
If ListBox1.ColumnCount < CountFields(rowFromFile, comma) Then
ListBox1.ColumnCount = CountFields(rowFromFile, comma)
End If
ListBox1.AddRow("")
For i = 1 To CountFields(rowFromFile, comma)
oneCell = NthField(rowFromFile, comma, i)
ListBox1.Cell(ListBox1.ListCount - 1, i - 1) = oneCell
Next
Wend
textInput.Close
[code] If f <> Nil And f.Exists Then
Dim comma As String = ChrB(44)
textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.WindowsANSI
textInput = textInput.ReplaceAll(comma, chr(9))
ListBox1.Cell(- 1, - 1) = textInput
textInput.Close
End If[/code]
Also, be aware that if in your text you have a comma, it will add one column at read time
There are also some other delimiters used (Tab, “;”, etc.) Get an eye at LibreOffice, they have a window to set stuff before loading.
I ask because my 84 years old sister still talks Francs, but the one before 1960 (There was a change in 1959, I think.) I am unsure about (except when she do the conversion).
there is a topic elsewhere on this forum (about a year ago) that discusses this in depth. That discussion resulted in the CSV parser that is built in to my Tadpole SQL Manager
I have found the solution with Chilkat plugin (free), tested read and extract (column values csv) in my application file with near then 1000 lines, running properly .
/ / year,color,country,food
// 2001,red,France,cheese
// 2005,blue,"United States",hamburger
// 2008,green,Italy,pasta
// 1998,orange,Japan,sushi
//
// The first row contains the column names.
// This file is available at:
// http://www.chilkatsoft.com/testData/sample.csv
Dim csv As New Chilkat.Csv
// Prior to loading the CSV file, indicate that the 1st row
// should be treated as column names:
csv.HasColumnNames = True
// Load the CSV records from the file:
Dim success As Boolean
success = csv.LoadFile("sample.csv")
If (success <> True) Then
System.DebugLog(csv.LastErrorText)
Return
End If
// Display the contents of the 3rd column (i.e. the country names)
Dim row As Int32
Dim n As Int32
n = csv.NumRows
For row = 0 To n - 1
System.DebugLog(csv.GetCell(row,0))
Next