Best way to start

Milford,

I put together a complete working example of how to read a CSV file. It also includes a class to read and write an old Windows style INI configuration file.

Here is the link to download a ZIP file with the Xojo program and an example INI file.
https://www.dropbox.com/s/ylctdkhkz9q1udp/CSVframework.zip?dl=0

I saved the program in Xojo Project format AND Xojo Binary format. I think the Binary format only works if you have a paid version of Xojo.

Things to watch out for:

  1. I tested this on a MacBook (OSX) but I think you are using Windows. This should work fine but there might be some “quirks”.

  2. Make sure the Copy File setting in the Build Settings in the IDE works. Those are found on the left column in the IDE and the Build Settings are on the bottom of that column. These copy a file (the sample INI file in this case) to where the executable file exists when it runs. You could put the INI file in some other directory and “hard code” where it was located. The code to read the INI file is in the APP.OPEN event and it currently assumes the INI file to be in the same directory as the executable. These don’t do anything useful for this example but I figured you might need them later. You can also examine them to help understand file reading/writing.

  3. IMPORTANT - Find the System.DebugLog viewer on the bottom of the IDE. It is the little icon on the bottom of the IDE Window that looks like a WiFi symbol leaning to the right. There is code you will see in the program that displays stuff for debugging. VERY HANDY, especially when trying to learn something and see how it works.

  4. This example lets you pick the desired CSV file from a popup dialog and defaults to looking for CSV files.

  5. It assumes that your sample CSV file starts with the column names in row one. When you open the CSV file it loads the column names into an array. There is also another array you may not need but I used it to determine if the column was a string or a number. I loaded it from another source and that code is NOT in this sample program.

  6. Once the CSV file is opened another button becomes active to load the data. It does not actually load anything but it does loop through the records and checks that the number of columns in the data rows match the number of columns in first row of names. When done it will display the record count or an error message if that happens.

  7. I do some special processing of the delimiters in the CSV file with a convert function (NormalizeDelimiters) I wrote. I assume commas in input but that could be changed. I then convert each row when read to change the delimiter to an ASCII Chr(0). I observe quoted (“xxx”) fields and keep the comma delimiters inside of pairs of quotes but I remove the quotes on the row that is returned by the convert function. This avoids the whole problem of commas and quotes in data fields.

  8. Take a look at the Array Append function. Look up Array in help (click the ? in the top of the Xojo IDE). Scroll to the bottom and click APPEND under the heading “See Also”. I don’t think Liberty BASIC has any equivalent and I have found it pretty handy in Xojo. It lets you add new elements to the end of an array without having to pre DIM the total size but I don’t think it works for multi-dimensional arrays.

Please dissect this program to understand it and don’t just use it and ignore how things are done. Read the WHOLE THING including the comments before you try any modifications.

I am a subscriber to xDev Magazine (Marc Zeedar’s post above) and the beginner article looks pretty useful. It has lots of info about the IDE in addition to the Xojo language itself. It would save you WAY over $10 in aspirin for headaches.

Eventually you will probably want to learn about SQLite for databases but for now see if you can take this skeleton and add some code to actually deal with the CSV file and do something useful. Remember you can put in lots of System.DebugLog statements to output messages and take “baby steps” to test your code. Make a few changes, print some stuff with System.DebugLog, and then repeat.

If you need more general help post here. If you have trouble with this example program also post here and I will try to help.

Enjoy,

Mark

Fyi… the Binary format works for all License Levels… and is the ONLY one that can be WRITTEN in the “Free IDE” version. All versions can READ all the formats

Dave,

Doh … I guess I had that backwards … an additional few seconds of thought I could have guessed better.

Thanks.

Thanks so much Mark. Just got home and will spend a few hours tonight and tomorrow going over the example. I’ll report back on how I do.

I tried to unzip the csv file and win zip will not open it. Not sure why. Says something is missing.

Can you make it available in text format so I can read through it. Or in a non zipped file format?

Thanks.

Use this link instead:

https://www.dropbox.com/s/ylctdkhkz9q1udp/CSVframework.zip?dl=1

The download starts immediately and you will get a zip file called CSVframework.zip.

The zip is opened and load the file “CSVframework.xojo_project” (from the generted folder) and run.

Then, click i the main window’s first button, locate a csv file and select it / and click in the second button.

After that, you can go to the code to know what it do.

WinZip says it can’t unzip because there is data missing and will unzip part, but will rename it Corrupt.

Use Windows 10 Zip built in utility. Or download a more recent version.

The zip file is 26.987 Bytes… in case your download was bad.

Did you try to re download the archive file ?

OK … here are two more links. One for the Xojo binary project and one for a sample INI file. Both are NOT ZIPPED. You should be able to load the CSVframework.xojo_binary_project directly into the Xojo IDE. You can open the MyConfigSettings.ini sample file with notepad.

https://www.dropbox.com/s/wirsj47z4rjjfyh/CSVframework.xojo_binary_project?dl=0

https://www.dropbox.com/s/47ft2agevk7vchr/MyConfigSettings.ini?dl=0

Let me know if you have trouble with these links and I will do something else.

Thank you Mark. I managed to load it into Xojo and am looking through all the code. Going to take me a while to understand I’m sure. The csv files I access do not have column names in the first row. We have the data file map and code off of it.

Sure I am going to have questions. I’ll be back…LOL

By the way. I did go ahead and buy the xDev magazine with the Xojo beginners article.

Milford,

Since you don’t have column names you can comment out that chunk of code in the FindTheFile method BUT you will also need to modify the data load method (LoadArray) since it assumes the number of names in the first row to be the number of columns in the data portion of the file. You can just modify the code like this:

'Current code

'Loop through the input row based on the number of columns found when ' the first row was processed by the FindTheFile method. for i = 0 to ColumnNames.Ubound

'To this code since the ColCount is already calculated a few lines above here

'Loop through the input row based on the number of columns found in ' the data row just read. for i = 0 to ColCount

I hope this helps you get this modified to work with your data.

Mark

Thanks. Yea I can hard code the col count because it’s 1435 columns.

One thing I noticed. You have file type and extension listed as CSV. But the files I use are extension jcp, made the change with no problem. But end users will access drf, or jcp or mcp depending on what data file provider they use. I tried to add all three in place of the csv and it didn’t work. How do I include all three extensions?

Put all of the possible file extensions in the Filetype Extension field separated with semi colons.

Like:

jcp;drf;mcp

The HELP info for Xojo has improved recently. Remember to check here:
http://documentation.xojo.com

Mark’s response is correct but if you’re on Windows it seems that the leading dot is necessary, as in:

.jcp;.drf;.mcp

Thanks guys. I have been sick and been away a couple of days. Trying to get better.

Mark,

I don’t understand why a bug shows up here.

'Open the input file
try
InPath = INIfile
InFile = new FolderItem(InPath,FolderItem.PathTypeNative)
*** BUG HERE*** InStream = TextInputStream.Open(InFile)
catch err as IOException
er = "Input File Error : " + str(err.ErrorNumber)
return “”
end try

no need to specifiy NEW with Folderitem
plus you should verify that InFile is both “Not Nil” and does “Exist”