Reading a CSV file (with commas in 1 of the fields)
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Re: Reading a CSV file (with commas in 1 of the fields)
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
Sorry, I'm a Newbie to LC
Re: Reading a CSV file (with commas in 1 of the fields)
Could you please post (some of) your code?
Re: Reading a CSV file (with commas in 1 of the fields)
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
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
-
- 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)
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.
...and the names of the coumns have to match the names in the first line of the csv file.
Re: Reading a CSV file (with commas in 1 of the fields)
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!
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.
Re: Reading a CSV file (with commas in 1 of the fields)
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
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
-
- 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)
or "set the dgText[false]..." just to see if anything gets into the datagrid.put url ("file:" & it) into theData
set the dgText[TRUE] of group "DataGrid 1" to theData
And yes, I missed the bit about the csv file possibly using commas. That would be a serious drawback.
Re: Reading a CSV file (with commas in 1 of the fields)
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.
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.
[SOLVED]Re: Reading a CSV file (with commas in 1 of the fiel
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 !
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 !
Re: Reading a CSV file (with commas in 1 of the fields)
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.
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.
Re: [SOLVED]Re: Reading a CSV file (with commas in 1 of the fiel
Hm, so obviously "the first line did not contain column names"!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 !
-
- 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)
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)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.
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
Targets: Mac, iOS
Re: Reading a CSV file (with commas in 1 of the fields)
And you can even omit the FALSE in that case:
...
set the dgText of group "Display" to theData
...
Best
Klaus
...
set the dgText of group "Display" to theData
...
Best
Klaus