How to extract comma delimited values

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

End If

[/code]

A title to the thread would be useful …

Have a look at the Split function in Xojo.

myArrayOfValues() = split(rowFromFile, ",")

Now use the array to place your values in the listbox cells

from my head

[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]

Strange; why not UTF8 ?

No On Try / Else End Try ?

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.

That WindowsANSI comes from the original question. maybe he need it.

Sorry, I do not noticed that.

Djamel ? Why WindowsANSI ?

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).

It was only for some test (forget) , it’s naturally running with UT8 …

Sorry for the forget !

remember, a simple “split” will fail to provide the correct results if one (or more) of the data elements contains a comma

yep you need a parser to open correctly csv files.

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

https://forum.xojo.com/40860-csv-parser/1

Thank’s Jean-Yves .
I see this subject with Norman Palardy csv codes …

If you use MBS Plugins, you can also try SplitCommaSeparatedValuesMBS function.

Hello Christian,
I which MBS this func. is available, there are lot of … , i will see if it interesting for me to buy all, i a not professional dev. !

If you click link, you will see it’s in our Util plugin.

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

This plugin is not free, but costs at least $289 USD.
You can get MBS Plugins currently in OmegaBundle.

[quote=393267:@Christian Schmitz]This plugin is not free, but costs at least $289 USD.
You can get MBS Plugins currently in OmegaBundle .[/quote]

Thank you Christian.