reading in a .csv file

Hi All.

I’m having a real stroke-out here. I’ve done this 100 times before in Realstudio, but can’t get it to work in Xojo.

I want to open a file and read in a csv file to put values into Textfields (though right now I’m thinking a listbox might be a better way to go).
I can open a dialog box, and navigate to my file, but no matter what I try, I can’t even read in the first field.

Can someone push me in the right direction.

Regards

show us how you are trying to do it

Hi Dave.

This is the code I have under my action for a button I push to go to and read in my .csv

[code]Sub Action()
dim f as FolderItem = GetFolderItem("\\MICHAELC-PC\Users\MichaelC\Documents")
dim tis as TextInputStream
dim st() as string
dim s as string
dim i as integer

if f <> nil and f.Exists then
Listbox1.DeleteAllRows
tis = TextInputStream.Open(f)
tis.Encoding = Encodings.UTF8
s = tis.ReadAll
st = split(s, EndOfLine)
end if

if tis.EOF = false then
Listbox1.AddRow
Listbox1.Cell( i , -1) = st(i).ReplaceAll(",", chr(9))
end

tis.Close
Listbox1.Heading(-1) = Listbox1.Cell( 0 , -1)
Listbox1.RemoveRow(0)
End Sub[/code]

Regards

looks to me like you only ever work with ROW ZERO (since the value of “i” is never changed that I can see)
and then you remove(0)

[code]
Sub Action()
dim f as FolderItem = GetFolderItem("\\MICHAELC-PC\Users\MichaelC\Documents")
dim tis as TextInputStream
dim st() as string
dim s as string
dim i as integer

if f <> nil and f.Exists then
Listbox1.DeleteAllRows
tis = TextInputStream.Open(f)
tis.Encoding = Encodings.UTF8
s = tis.ReadAll
tis.Close
st = split(s, EndOfLine)
for i=0 to st.ubound
Listbox1.AddRow
Listbox1.Cell( i , -1) = st(i).ReplaceAll(",", chr(9))
next i
end if

Listbox1.Heading(-1) = Listbox1.Cell( 0 , -1)
Listbox1.RemoveRow(0)
End Sub

Also… you are “assuming” that you data will NEVER have a comma inside the data, and that nothing is QUOTED

Try this…
tis = TextInputStream.Open(f)
tis.Encoding = Encodings.UTF8
s = tis.ReadAll

tis = TextInputStream.Open(f)
s = tis.ReadAll
S = S. DefineEncodings(Encodings.UTF8)

Lennox

  dim f as FolderItem = GetFolderItem("\\\\MICHAELC-PC\\Users\\MichaelC\\Documents")

It doesn’t look like a path to a file. More looks like a folder

I’d suggest you look at this thread and use a more intelligent pre-built class to do the parsing for you.

https://github.com/npalardy/CSVParser

Hi Norman.

Sorry for looking like an idiot, but I downloaded the CSVParser.
How do I use it?

Regards

its in an example app
open the projhect and copy out the csvparser and put it in your code
then use the example project as a guide

Ok, but when I go to open the project, with the file, it keeps staying at a point that says "Loading simple CSV and asks me to select another file, but not telling me which file I have to select.

Regard

dont RUN the project
open the code and read the code in it
get a feel for how you use the code first

I DID IT.

Now for the REALLY embarrassing reason.
The file even though it SHOWED commas… it really used TABS!

Thanks for all your help guys.

Regards

awesome :slight_smile:
and you can alter csv reader to use a tab separator if you need :stuck_out_tongue:

One little appending… just thought of this.

My users will undoubtedly want to use the program for different “csv” files. As stated above what I thought was a comma separator was actually a TAB. So on my window I have 3 checkboxes one for commas, one for spaces, one for tabs.

I have created a select case, as follows:

select case index case 0 MsgBox "You clicked the first checkbox" 'tab = changeSeparator("44") CheckBox1(2).Value = False Checkbox1(1).Value = False case 1 MsgBox "You clicked the second checkbox" 'tab = changeSeparator("9") CheckBox1(2).Value = False Checkbox1(3).Value = False case 2 MsgBox "You clicked the thired checkbox" 'tab = changeSeparator("32") CheckBox1(0).Value = False Checkbox1(1).Value = False end select

I know when I click the checkbox, I hit the right case. Great.

Now since I want to pass each of these values for different separators to the same variable I created a global module with the name I wanted, with a value as

changeSeparator
Parameters : tab as string
Return type : string
Scope : Global

However, I never seem to able able to get the value to get sent to the window.

Any ideas?

Regards

In general for desktop applications, I prefer RadioButtons in a GroupBox as they are automatically mutually exclusive, while checkboxes are great when you can select any combination of entries.

There is some sample code in the RadioButton documentation that shows you can access which choice the user made. I’d suggest you setup a group box with radio buttons for each delimiter choice, making one of them the default.

But to more specifically address your question, your code block does not show how you get “index” the in the SELECT CASE statement, or where this code is located other than “on the window”. So (to me) it is too ambiguous to answer the question asked. That said, I’d consider refactoring to radio buttons then see if you have more follow up questions.

Hi Douglas.

I get the select case as I set up the checkboxes as a control group.

I will try the radio button option you gave me as well and advise.

Regards.

Hi All.

Well, I got the separator issue fixed. Thanks to everyone for their help so far.

Instead of setting up a global module and a method, I just created a Property and it works fine.
Man, why am I always trying to do things the hard way?

Anyway, that brings up this question…
When I tried to use the global module and method, I could never pass the variable to my pushbutton event.
Why would that be?

I’ve read the Introduction to Xojo book, and I think I am doing everything correctly.

My settings are shown in an earlier post.

Regards