Reading a CSV file (with commas in 1 of the fields)

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

derr1ck
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Wed Mar 30, 2011 11:14 pm

Re: Reading a CSV file (with commas in 1 of the fields)

Post by derr1ck » Tue Oct 25, 2011 4:39 pm

I still can't see any of my data displayed in the data grid. I can see that the data is being read from the .csv file via debugging, What do I need to add to this code to display the data on screen?.

Sorry, I'm a Newbie to LC :)

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Reading a CSV file (with commas in 1 of the fields)

Post by Klaus » Tue Oct 25, 2011 5:20 pm

Could you please post (some of) your code?

derr1ck
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Wed Mar 30, 2011 11:14 pm

Re: Reading a CSV file (with commas in 1 of the fields)

Post by derr1ck » Tue Oct 25, 2011 6:14 pm

on mouseUp
answer file "file path to .csv file"
put url ("file:" & it) into theData
put true into firstLineContainsColumnNames
-- set the dgText[firstLineContainsColumnNames] of group "Display" to theData

end mouseUp

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Reading a CSV file (with commas in 1 of the fields)

Post by mwieder » Tue Oct 25, 2011 6:24 pm

Have you previously defined your column names? I ran into that problem expecting firstLineContainsColumnNames to set them up for me and ended up with a blank datagrid.

...and the names of the coumns have to match the names in the first line of the csv file.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Reading a CSV file (with commas in 1 of the fields)

Post by sturgis » Tue Oct 25, 2011 6:30 pm

The code refers back to this post by MSmith I think, which is talking about importing a tab delimited file, not a csv comma delimited. Converting it to tab delim rather than csv would be the first step since (iirc) the datagrid dgtext takes tab delimited rows, if you haven't done so already. (ok so it could be tab delim by default, but wanted to mention it.)

And what Mark W said too!
marksmithhfx wrote:BTW, after reading Richard's examples of processing CSV I'm even more impressed with how easy it is to import a tab delimited file into a datagrid and display it in LC. For the record, here are 4 lines of code that will do that (the only prep is that you must drag a datagrid onto a card and specify the column names in the property inspector):

answer file ""
put url ("file:" & it) into theData
put true into firstLineContainsColumnNames -- or false, depending
set the dgText[firstLineContainsColumnNames] of group "DataGrid 1" to theData

Tip: if you have column names in your data (i.e. specify TRUE above) you can rearrange how LC displays the columns by specifying a different order in the property inspector. If your named columns are a, b, c and you specify c, b, a in the inspector, LC will display them in the order you specify. Pretty slick.

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Reading a CSV file (with commas in 1 of the fields)

Post by Klaus » Tue Oct 25, 2011 8:20 pm

Hi folks,

yes, "the dgtext of grp xyz" requires TAB delimited text and
set the the dgText[firstLineContainsColumnNames]" to XYZ
requires that the columns are already defined and present in that datagrid!

But I really wonder if one can spare some typing and just do:
...
put url ("file:" & it) into theData
set the dgText[TRUE] of group "DataGrid 1" to theData
## :)
...

Best

Klaus

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

Re: Reading a CSV file (with commas in 1 of the fields)

Post by mwieder » Tue Oct 25, 2011 9:18 pm

put url ("file:" & it) into theData
set the dgText[TRUE] of group "DataGrid 1" to theData
or "set the dgText[false]..." just to see if anything gets into the datagrid.

And yes, I missed the bit about the csv file possibly using commas. That would be a serious drawback.

derr1ck
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Wed Mar 30, 2011 11:14 pm

Re: Reading a CSV file (with commas in 1 of the fields)

Post by derr1ck » Wed Oct 26, 2011 10:03 am

Yes, I have saved the filed as Tab delimited, and currently have this code on a button:-

answer file "/file/path/myfile.csv"
put url ("file:" & it) into theData
put true into firstLineContainsColumnNames -- or false, depending
set the dgText[true] of group "Display" to theData


Display is the name of the DataGrid which also has the same column names as the 1st line with the column name within the data file.
But still no data appears in the data grid.

derr1ck
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Wed Mar 30, 2011 11:14 pm

[SOLVED]Re: Reading a CSV file (with commas in 1 of the fiel

Post by derr1ck » Wed Oct 26, 2011 10:07 am

I just changed

answer file "/path/to/file/myfile.csv"
put url ("file:" & it) into theData
put false into firstLineContainsColumnNames -- or false, depending
set the dgText[false] of group "Display" to theData


and it worked ! :)

derr1ck
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 25
Joined: Wed Mar 30, 2011 11:14 pm

Re: Reading a CSV file (with commas in 1 of the fields)

Post by derr1ck » Wed Oct 26, 2011 11:28 am

Ah, Now Just one more thing..............

How can I now save the data form the datagrid to a new sqlite/mysql file.

I'm trying to build an inventory program and have so far transferred contents from a excel file and now want to populate a sqlite or mysql database with that data and then allow a user to continue adding new content to the database.

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: [SOLVED]Re: Reading a CSV file (with commas in 1 of the fiel

Post by Klaus » Wed Oct 26, 2011 12:11 pm

derr1ck wrote:I just changed

answer file "/path/to/file/myfile.csv"
put url ("file:" & it) into theData
put false into firstLineContainsColumnNames -- or false, depending
set the dgText[false] of group "Display" to theData

and it worked ! :)
Hm, so obviously "the first line did not contain column names"! 8)

marksmithhfx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 931
Joined: Thu Nov 13, 2008 6:48 am
Location: London, UK

Re: Reading a CSV file (with commas in 1 of the fields)

Post by marksmithhfx » Mon Oct 31, 2011 1:49 am

derr1ck wrote:Ah, Now Just one more thing..............

How can I now save the data form the datagrid to a new sqlite/mysql file.

I'm trying to build an inventory program and have so far transferred contents from a excel file and now want to populate a sqlite or mysql database with that data and then allow a user to continue adding new content to the database.
Derrick, you might try using the datagrid to view/browse data, even select a record for edit/entry but do the actual manipulation using other tools and techniques (like using edit/entry forms). Search for CRUD on this list (create, read, update, delete) which is a very extensive sqlite example created by townsend. You''ll want to find a msg from townsend that has a file attachment you can download (there is one on Mon May 30th, but there may be others). There is also a video demo available (might still be available from here: http://dl.dropbox.com/u/35256958/CRUD-Example-video.mp4)

Finally, Devin Asay has a very good tutorial online which shows how to integrate a database with a datagrid which you may find useful:

http://livecode.byu.edu/database/dbmanagementinrev.php

PS just a note that if you are going to "hard code" false into the statement ** set the dgText[false] of group "Display" to theData ** you won't need the preceding statement "put false into firstLineContainsColumnNames -- or false, depending".

Cheers,

-- Mark
macOS 12.6.5 (Monterey), Xcode 14.2, LC 10.0.0, iOS 15.6.1
Targets: Mac, iOS

Klaus
Posts: 13878
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Reading a CSV file (with commas in 1 of the fields)

Post by Klaus » Mon Oct 31, 2011 12:29 pm

And you can even omit the FALSE in that case:
...
set the dgText of group "Display" to theData
...
:D


Best

Klaus

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”