G GuenterL Member Licensed User Longtime User Feb 6, 2016 #1 Hi all, I do have a short question: I want to fill up a tableview with values stored in a comma separated ascii file. The file valus look like: ... Value,Value,Value ... Values can be empty. Now it try to read the file and import the data into a tableview using the following code: Code: Dim line As String Dim str() As String If fileName <> "" Then Reader.Initialize(File.OpenInput("", fileName)) Do While line <> Null line = Reader.ReadLine str = Regex.Split(",", line) Log(str(0)) TableView1.Items.Add(str) Loop Reader.Close End If The log display correctly the first value, but it seems, that str(1) and str(2) are empty. What do i wrong ?? I am struggeling with this since 2 days !! Thanks for your help in advance GuenterL
Hi all, I do have a short question: I want to fill up a tableview with values stored in a comma separated ascii file. The file valus look like: ... Value,Value,Value ... Values can be empty. Now it try to read the file and import the data into a tableview using the following code: Code: Dim line As String Dim str() As String If fileName <> "" Then Reader.Initialize(File.OpenInput("", fileName)) Do While line <> Null line = Reader.ReadLine str = Regex.Split(",", line) Log(str(0)) TableView1.Items.Add(str) Loop Reader.Close End If The log display correctly the first value, but it seems, that str(1) and str(2) are empty. What do i wrong ?? I am struggeling with this since 2 days !! Thanks for your help in advance GuenterL
rwblinn Well-Known Member Licensed User Longtime User Feb 6, 2016 #2 Hi, try like Code: Sub FillTableView Dim line As String Dim l As List = File.ReadList(File.DirApp, "test.csv") For Each line As String In l TableView1.Items.Add(Regex.Split(",", line)) Next End Sub Upvote 0
Hi, try like Code: Sub FillTableView Dim line As String Dim l As List = File.ReadList(File.DirApp, "test.csv") For Each line As String In l TableView1.Items.Add(Regex.Split(",", line)) Next End Sub
G GuenterL Member Licensed User Longtime User Feb 6, 2016 #3 Thanks for the swift answer. Very interesting, i used your code,but NOTHING happens !! May be the problem is something totally different. Do I have to update the tableview, because the tableview is displayed empty, but trying to enter another value manually, it ask to overright existing (not visible) values. Another idea ?? Guenter Upvote 0
Thanks for the swift answer. Very interesting, i used your code,but NOTHING happens !! May be the problem is something totally different. Do I have to update the tableview, because the tableview is displayed empty, but trying to enter another value manually, it ask to overright existing (not visible) values. Another idea ?? Guenter
DonManfred Expert Licensed User Longtime User Feb 6, 2016 #4 GuenterL said: Another idea ?? Click to expand... Create a small project which shows the problem. Export it as zip and upload it here. Seeing (and running) the code is most probably easier to find the problem than from code snippets Upvote 0
GuenterL said: Another idea ?? Click to expand... Create a small project which shows the problem. Export it as zip and upload it here. Seeing (and running) the code is most probably easier to find the problem than from code snippets
rwblinn Well-Known Member Licensed User Longtime User Feb 6, 2016 #5 Attached the testproject I used: Defined the tableview with the B4J Internal Designer, then run filltableview when starting the project. No more changes to the tableview made. Hope this helps. Attachments TableViewImportCSV.zip TableViewImportCSV.zip 2.4 KB · Views: 395 Upvote 0
Attached the testproject I used: Defined the tableview with the B4J Internal Designer, then run filltableview when starting the project. No more changes to the tableview made. Hope this helps.
G GuenterL Member Licensed User Longtime User Feb 6, 2016 #6 Hi Rob, i tried your testproject... and it works fine. I tried it also with my file, and it fails The file is called test.txt and looks like this: 00:06:18,, 00:03:56,1.4,21.3 00:02:05,0.8,22.9 00:11:18,, 00:04:17,1.6,22.4 strange...... Guenter Upvote 0
Hi Rob, i tried your testproject... and it works fine. I tried it also with my file, and it fails The file is called test.txt and looks like this: 00:06:18,, 00:03:56,1.4,21.3 00:02:05,0.8,22.9 00:11:18,, 00:04:17,1.6,22.4 strange...... Guenter
Daestrum Expert Licensed User Longtime User Feb 6, 2016 #7 Did you create the tableview in code or via the designer? If you created it via code the columns get squished unless you give them a width, so it looks like the table is empty. Try dragging the column headers over (the mouse changes to a <-> on the column boundaries) to see if you can see your data in the table. Using your data. Code: For Each line As String In Regex.Split(Chr(10),File.ReadString("../","tst.csv")) tv.Items.Add(Regex.Split(",",line)) Next Last edited: Feb 6, 2016 Upvote 0
Did you create the tableview in code or via the designer? If you created it via code the columns get squished unless you give them a width, so it looks like the table is empty. Try dragging the column headers over (the mouse changes to a <-> on the column boundaries) to see if you can see your data in the table. Using your data. Code: For Each line As String In Regex.Split(Chr(10),File.ReadString("../","tst.csv")) tv.Items.Add(Regex.Split(",",line)) Next
G GuenterL Member Licensed User Longtime User Feb 6, 2016 #8 Hi, the tableview was created in designer. But there is a strange other issue: The example from Rob aslo works on my machine. But using my file, if fails Java Error says: "The system cannot find the file specified" ??????? Using same dir, same user (administrator) file permission is the same csv works fine, txt fails guenter Upvote 0
Hi, the tableview was created in designer. But there is a strange other issue: The example from Rob aslo works on my machine. But using my file, if fails Java Error says: "The system cannot find the file specified" ??????? Using same dir, same user (administrator) file permission is the same csv works fine, txt fails guenter
Daestrum Expert Licensed User Longtime User Feb 6, 2016 #9 Silly question, do you have the file open in another app ? it may have an exclusive lock on it. Last edited: Feb 6, 2016 Upvote 0
rwblinn Well-Known Member Licensed User Longtime User Feb 6, 2016 #10 The issue is with the csv file. all cells must be filled, else regex.split returns not all cells as expected by the tableview (idc 3 cells). If the csv file contains at least a blank for each cell then it works (like below). This means, that if the csv file has different number of filled cells per row, the code has to cope with that = per line checks are required to then set the tablerow cells accordingly. Code: 00:06:18, , 00:03:56,1.4,21.3, 00:02:05,0.8,22.9, 00:11:18, , 00:04:17,1.6,22.4, Upvote 0
The issue is with the csv file. all cells must be filled, else regex.split returns not all cells as expected by the tableview (idc 3 cells). If the csv file contains at least a blank for each cell then it works (like below). This means, that if the csv file has different number of filled cells per row, the code has to cope with that = per line checks are required to then set the tablerow cells accordingly. Code: 00:06:18, , 00:03:56,1.4,21.3, 00:02:05,0.8,22.9, 00:11:18, , 00:04:17,1.6,22.4,
Daestrum Expert Licensed User Longtime User Feb 6, 2016 #11 Odd, it works the same for me whether the file is called .csv or .txt and without spaces between the commas for blank items, and without the trailing comma on each line. Upvote 0
Odd, it works the same for me whether the file is called .csv or .txt and without spaces between the commas for blank items, and without the trailing comma on each line.
rwblinn Well-Known Member Licensed User Longtime User Feb 6, 2016 #12 Deastrum: your code at post #7 works fine, means using file.readstring instead of file.readlist. Good learning. Thanks for that. Testproject in Post #5 updated. Upvote 0
Deastrum: your code at post #7 works fine, means using file.readstring instead of file.readlist. Good learning. Thanks for that. Testproject in Post #5 updated.
G GuenterL Member Licensed User Longtime User Feb 6, 2016 #13 Good New, bad news First, it works fine now, it loads the values and fills the table. But after that, it locks the whole application, so no additional action is possible. Do I have to close the file (like in B4a ?) Attached now, the code of the whole module. Thanks in advance Guenter PS.: where is the logfile stored. I am not able to copy/paste it from the editor. Code: Sub mnuLoad_Action Dim ret As String Dim fc As FileChooser fc.Initialize fc.InitialDirectory = File.DirApp fc.Title = "Load Time list" fc.SetExtensionFilter("Sequence", Array As String("*.seq")) If TableView1.Items.Size > 0 Then ret = msgBox.Show2("overwrite existing list?","", "Yes", "No", "") If ret = DialogResponse.POSITIVE Then TableView1.Items.Clear Else Return End If End If Dim fileName As String = fc.Showopen(MainForm) Dim line As String Dim str() As String If fileName <> "" Then For Each line As String In Regex.Split(Chr(10),File.ReadString("",fileName)) TableView1.Items.Add(Regex.Split(",",line)) Next End If End Sub Upvote 0
Good New, bad news First, it works fine now, it loads the values and fills the table. But after that, it locks the whole application, so no additional action is possible. Do I have to close the file (like in B4a ?) Attached now, the code of the whole module. Thanks in advance Guenter PS.: where is the logfile stored. I am not able to copy/paste it from the editor. Code: Sub mnuLoad_Action Dim ret As String Dim fc As FileChooser fc.Initialize fc.InitialDirectory = File.DirApp fc.Title = "Load Time list" fc.SetExtensionFilter("Sequence", Array As String("*.seq")) If TableView1.Items.Size > 0 Then ret = msgBox.Show2("overwrite existing list?","", "Yes", "No", "") If ret = DialogResponse.POSITIVE Then TableView1.Items.Clear Else Return End If End If Dim fileName As String = fc.Showopen(MainForm) Dim line As String Dim str() As String If fileName <> "" Then For Each line As String In Regex.Split(Chr(10),File.ReadString("",fileName)) TableView1.Items.Add(Regex.Split(",",line)) Next End If End Sub
Daestrum Expert Licensed User Longtime User Feb 7, 2016 #14 As far as I am aware the readstring simply reads then closes the file. Try removing the Code: Dim line As String line as the for each line defines the line variable too. It shouldn't make a difference though. As Rob said earlier There is something weird going on with the split. The split is introducing an extra CHR(13) so you get eg from 1,, split on , (array 0) 1 (array 1) a zero length item as expected (array 2) chr(13) - not the zero length item I would expect. It's not reading the chr(13) from the file as I replaced all CRLF with chr(10) in the string from the file. Most odd...... Last edited: Feb 7, 2016 Upvote 0
As far as I am aware the readstring simply reads then closes the file. Try removing the Code: Dim line As String line as the for each line defines the line variable too. It shouldn't make a difference though. As Rob said earlier There is something weird going on with the split. The split is introducing an extra CHR(13) so you get eg from 1,, split on , (array 0) 1 (array 1) a zero length item as expected (array 2) chr(13) - not the zero length item I would expect. It's not reading the chr(13) from the file as I replaced all CRLF with chr(10) in the string from the file. Most odd......
Erel B4X founder Staff member Licensed User Longtime User Feb 7, 2016 #15 You should use StringUtils.LoadCSV to read a CSV file. Upvote 0
rwblinn Well-Known Member Licensed User Longtime User Feb 7, 2016 #16 Pls find attached a tested version using the jStringUtils library. Attachments TableViewImportCSV.zip TableViewImportCSV.zip 2.4 KB · Views: 436 Upvote 0
Daestrum Expert Licensed User Longtime User Feb 7, 2016 #17 As an aside - would it not be logical to have the other file reading routines like loadcsv etc in the File.??? routine. Upvote 0
As an aside - would it not be logical to have the other file reading routines like loadcsv etc in the File.??? routine.
Erel B4X founder Staff member Licensed User Longtime User Feb 7, 2016 #18 StringUtils.LoadCSV implementation is much larger and more complex than the File related methods. I also not consider reading CSV as a core feature. So it is better to have it in a different library and not include it in the core library. BTW, you can easily find this feature with a single search: https://www.b4x.com/android/forum/pages/results/?query=load+csv&page=1&prefix=0 Upvote 0
StringUtils.LoadCSV implementation is much larger and more complex than the File related methods. I also not consider reading CSV as a core feature. So it is better to have it in a different library and not include it in the core library. BTW, you can easily find this feature with a single search: https://www.b4x.com/android/forum/pages/results/?query=load+csv&page=1&prefix=0
G GuenterL Member Licensed User Longtime User Feb 7, 2016 #19 Erel, your right !! (again). Why sould I redesign the wheel. I replaced my load/save stuff with Load/SaveCSV and everything was fine. Thnaks for your help Guenter Upvote 0
Erel, your right !! (again). Why sould I redesign the wheel. I replaced my load/save stuff with Load/SaveCSV and everything was fine. Thnaks for your help Guenter