AI2 - File component: reading from CSV file

2,101 views
Skip to first unread message

sn....@gmail.com

unread,
Sep 9, 2014, 10:22:01 AM9/9/14
to mitappinv...@googlegroups.com
Hi,

I try to read a CSV file in my program.

What I can do:
- place a CSV file on my Samsung GT-i8160
- find it (I place it in /sdcard0)
- with Call file1.ReadFrom I cant get the file in my program
- When file1 GotText is TRUE
- set MyList to text (from last command)

I 'THINK' I must use 'list to csv table list'

Problem
I get un Runtime error saying something like:
'unable to read file with 'list to csv table list'

I am out op options because I can't find a proper example with file1, 'list to csv table list' and 'list to csv row list'

So now I am turning to you :-)

TIA


SteveJG

unread,
Sep 9, 2014, 10:35:55 AM9/9/14
to mitappinv...@googlegroups.com
Post a piece of your csv file text.  That will show us the csv structure and help us here to understand what you want to convert.   Some blocks with a DoIt would be most helpful.

Once you have the list in your Android, you need to read the csv, which is a text file.    Use the File.Read    then to convert it to a list, use   list from csv row text  (if your list looks like this:    apples, pears cherries, oranges )   or list from csv table text if your csv looks like   apples, pears cherries, oranges \n\n carrots, peas, lettuce, cleary \n\n        You probably have the first instance.

Try some blocks.

Regards,
Steve
Message has been deleted

SteveJG

unread,
Sep 9, 2014, 3:52:39 PM9/9/14
to
Try 

list from csv table text      with

Een1,Een2,Een3,Een4\n
Twee1,Twee2,Twee3,Twee4\n
Drie1,Drie2,Drie3,Drie4\n
Vier1,Vier2,Vier3,Vier4\n
Vijf1,Vijf2,Vijf3,Vijf4\n
Zes1,Zes2,Zes3,Zes4

The \n    is an Android carriage return. I believe I was wrong when I mentioned \n\n.  

Run the app, then do a DoIT  on the line and you will see a list of lists.

Try it.


sn....@gmail.com

unread,
Sep 9, 2014, 3:56:51 PM9/9/14
to mitappinv...@googlegroups.com

Hi Steve,

thanks for your quick reply :-)

NB
I removed my previous post because I attached an image in the wrong way.
This time it will go better.

I send an image of my blocks (see image).

I started with a (too?) big file that I had downloaded ---> that ended in a runtime error.
Then I made a csv file with a text ecitor ---> that ended in a runtime error too.
(see image)


This is the tekst file I made:
---------------------------
Een1,Een2,Een3,Een4
Twee1,Twee2,Twee3,Twee4
Drie1,Drie2,Drie3,Drie4
Vier1,Vier2,Vier3,Vier4
Vijf1,Vijf2,Vijf3,Vijf4
Zes1,Zes2,Zes3,Zes4

--------------------------

In real life it will be a spreadsheet kind of file (CSV actually :-)
On the rows are the events I like to investigate an
in the fields of the row are the pieces of data that I want to investigate.

NB
I noticed that you have \n\n in your file.
I can't see them in my file.

My system
iMac with Mac OS X.
My guess is that my lines have a Mac kind of CR.
But I don't know what Apple put there :-(

If you need more information...


TIA

sn....@gmail.com

unread,
Sep 9, 2014, 6:59:31 PM9/9/14
to mitappinv...@googlegroups.com

Hi Steve,

as you can see, there is data separated with "," and "\n".

That didn't work. I also ran into a runtime error.

I looked into the file on a deeper level (Darwin unix with vi) and I found a ^M as a CR between the records.
I removed these also, but still the same problem.

IF there are different types of CR in CSV files
THEN it must be possible to mark the delimiters

It ain't a problem for spreadsheet programs like Excel :-s

I want to download a csv-file from the internet and I like to think that it may not matter how it is delimited.
(The ";" is also widely used (by default) instead of the "," between fields in a row.)

Can you send me a working csv file (zipped)?

TIA

Taifun

unread,
Sep 9, 2014, 7:36:27 PM9/9/14
to mitappinv...@googlegroups.com
use list from csv table instead of list to csv table
Taifun

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.         

sn....@gmail.com

unread,
Sep 10, 2014, 10:16:38 AM9/10/14
to mitappinv...@googlegroups.com
Hi Steve,

I saw in your example that I should have used "list FROM csv table".
That did the trick.

Thanks :-)

sn....@gmail.com

unread,
Sep 10, 2014, 10:17:50 AM9/10/14
to mitappinv...@googlegroups.com
Hi Taifun,

After I changed that, reading was a lot easier :-)

Thanks :-)

SteveJG

unread,
Sep 9, 2014, 7:28:15 PM9/9/14
to mitappinv...@googlegroups.com
Perhaps this will help:





Remember, you are converting the csv to a List.  You will need it in a List to manipulate the csv in your app.  Regarding importing a csv from another source, if the file is just comma separated, it is easy but if you have to remove a preexisting CR, the block above will help you 'convert' it to a form AI2 can use.

What I do not show here is how to use the File component to Read a csv file into the blocks that I dragged and dropped your simple csv.   

OK ... here are the blocks to do that too:    


Try some more blocks.

-- Steve
Reply all
Reply to author
Forward
0 new messages