B Branko Milosevic Active Member Licensed User May 1, 2017 #1 I need to read CSV from a text file line by line. A mix of string and number types in each line and every line is same format. File has to be in this format and editable by hand. this is the file format: string, integer, double, string, integer string, integer, double, string, integer string, integer, double, string, integer string, integer, double, string, integer ....... Seems to many options to read text files but can't find a simple one for this.
I need to read CSV from a text file line by line. A mix of string and number types in each line and every line is same format. File has to be in this format and editable by hand. this is the file format: string, integer, double, string, integer string, integer, double, string, integer string, integer, double, string, integer string, integer, double, string, integer ....... Seems to many options to read text files but can't find a simple one for this.
klaus Expert Licensed User Longtime User May 1, 2017 #2 Did you have a look at LoadCSV in the StringUtils library? Upvote 0
freedom2000 Well-Known Member Licensed User Longtime User May 1, 2017 #3 or try string function library : Split (CurrentString As String, Split_At_Delimiter As String) As List Returns a LIST of items from the string split at the Delimiter. EXAMPLE: Dim ANS As List ANS = SF.Split("This is a test string.", " ") Code: Dim strSource As String = "-1.4985018,52.4053273,1983688820,node" Dim strDivisor = "," result.Initialize result = sf.Split(strSource, strDivisor) Log (result) (ArrayList) [-1.4985018, 52.4053273, 1983688820, node] Upvote 0
or try string function library : Split (CurrentString As String, Split_At_Delimiter As String) As List Returns a LIST of items from the string split at the Delimiter. EXAMPLE: Dim ANS As List ANS = SF.Split("This is a test string.", " ") Code: Dim strSource As String = "-1.4985018,52.4053273,1983688820,node" Dim strDivisor = "," result.Initialize result = sf.Split(strSource, strDivisor) Log (result) (ArrayList) [-1.4985018, 52.4053273, 1983688820, node]
B Branko Milosevic Active Member Licensed User May 1, 2017 #4 klaus said: Did you have a look at LoadCSV in the StringUtils library? Click to expand... That should work, Thanks. Upvote 0
klaus said: Did you have a look at LoadCSV in the StringUtils library? Click to expand... That should work, Thanks.
Didier9 Well-Known Member Licensed User Longtime User May 1, 2017 #5 There is more than one way to do it I use RegEx.Split as below: Code: Dim s as String s = "This,is,a,test" Dim item() As String = Regex.Split("\,", s) After this you find: Code: item(0) = "This" item(1) = "is" ... Upvote 0
There is more than one way to do it I use RegEx.Split as below: Code: Dim s as String s = "This,is,a,test" Dim item() As String = Regex.Split("\,", s) After this you find: Code: item(0) = "This" item(1) = "is" ...
Erel B4X founder Staff member Licensed User Longtime User May 2, 2017 #6 Regex.Split is not a good solution for a general CSV file. It will only work with simple values that are not escaped. Upvote 0
Regex.Split is not a good solution for a general CSV file. It will only work with simple values that are not escaped.
B Branko Milosevic Active Member Licensed User May 2, 2017 #7 Erel said: Regex.Split is not a good solution for a general CSV file. It will only work with simple values that are not escaped. Click to expand... LoadCSV is guving me string out of boudms error. Upvote 0
Erel said: Regex.Split is not a good solution for a general CSV file. It will only work with simple values that are not escaped. Click to expand... LoadCSV is guving me string out of boudms error.
klaus Expert Licensed User Longtime User May 2, 2017 #8 You shoud show us your code and provide a csv file. Without seeing what exactly you have done it's impossible to help! Last edited: May 2, 2017 Upvote 0
You shoud show us your code and provide a csv file. Without seeing what exactly you have done it's impossible to help!
B Branko Milosevic Active Member Licensed User May 2, 2017 #9 LoadCSV is giving me string index out of bounds error. Upvote 0
DonManfred Expert Licensed User Longtime User May 2, 2017 #10 Branko Milosevic said: LoadCSV is giving me string index out of bounds error. Click to expand... klaus said: You shoud show ur your code and provide a csv file. Without seeing what exactly you have done it's impossible to help! Click to expand... !!! Upvote 0
Branko Milosevic said: LoadCSV is giving me string index out of bounds error. Click to expand... klaus said: You shoud show ur your code and provide a csv file. Without seeing what exactly you have done it's impossible to help! Click to expand... !!!
B Branko Milosevic Active Member Licensed User May 2, 2017 #11 klaus said: You shoud show us your code and provide a csv file. Without seeing what exactly you have done it's impossible to help! Click to expand... Here is the code: Code: 'in global process Dim su As StringUtils 'in activity create Dim HoopsLibrary As List HoopsLibrary = su.LoadCSV(File.DirAssets, "hoopslibrary.txt", ",") log(HoopsLibrary) And the file: Code: FILE: hoopslibrary.txt 1, One, 1.5, 5, true, false 2, Two, 3, 5, false, false 3, Three, 2.5, 5, false, false 4, Four, 1.5, 5, true, false It fails at LoadCSV line with string index out of bounds error. Upvote 0
klaus said: You shoud show us your code and provide a csv file. Without seeing what exactly you have done it's impossible to help! Click to expand... Here is the code: Code: 'in global process Dim su As StringUtils 'in activity create Dim HoopsLibrary As List HoopsLibrary = su.LoadCSV(File.DirAssets, "hoopslibrary.txt", ",") log(HoopsLibrary) And the file: Code: FILE: hoopslibrary.txt 1, One, 1.5, 5, true, false 2, Two, 3, 5, false, false 3, Three, 2.5, 5, false, false 4, Four, 1.5, 5, true, false It fails at LoadCSV line with string index out of bounds error.
klaus Expert Licensed User Longtime User May 2, 2017 #12 It would be easier for us to help if you posted your project as a zip file. Otherwise we need to make a new project and make the csv file etc. which is consuming our time! With your project we can test exactly in the same conditions as you do. Upvote 0
It would be easier for us to help if you posted your project as a zip file. Otherwise we need to make a new project and make the csv file etc. which is consuming our time! With your project we can test exactly in the same conditions as you do.
eps Expert Licensed User Longtime User May 2, 2017 #14 What type of text file is the CSV file? Ideally check it using Notepad++ or similar - to ensure that it is a valid text file. I think @Erel posted a solution for parsing a csv file before, here : https://www.b4x.com/android/forum/t...ljava-lang-string-2bc5dbc0.29655/#post-172099 It may well be that you're parsing the file incorrectly, try the code above and debug it - the log will show you if the lines are being processed as you expect. Upvote 0
What type of text file is the CSV file? Ideally check it using Notepad++ or similar - to ensure that it is a valid text file. I think @Erel posted a solution for parsing a csv file before, here : https://www.b4x.com/android/forum/t...ljava-lang-string-2bc5dbc0.29655/#post-172099 It may well be that you're parsing the file incorrectly, try the code above and debug it - the log will show you if the lines are being processed as you expect.
B Branko Milosevic Active Member Licensed User May 2, 2017 #15 klaus said: It would be easier for us to help if you posted your project as a zip file. Otherwise we need to make a new project and make the csv file etc. which is consuming our time! With your project we can test exactly in the same conditions as you do. Click to expand... I will, I need to extract it from a larger project and will zip it and send it over if I don't find solution. I am not at the computer with the license at the moment. Will try to make sure the file is correct as well. It's created in the stock ms notepad. Upvote 0
klaus said: It would be easier for us to help if you posted your project as a zip file. Otherwise we need to make a new project and make the csv file etc. which is consuming our time! With your project we can test exactly in the same conditions as you do. Click to expand... I will, I need to extract it from a larger project and will zip it and send it over if I don't find solution. I am not at the computer with the license at the moment. Will try to make sure the file is correct as well. It's created in the stock ms notepad.
B Branko Milosevic Active Member Licensed User May 2, 2017 #16 frasc said: What are you using to generate the CSV file? Click to expand... Using stock windows notepad to make the file. Upvote 0
frasc said: What are you using to generate the CSV file? Click to expand... Using stock windows notepad to make the file.
DonManfred Expert Licensed User Longtime User May 2, 2017 #17 Branko Milosevic said: Using stock windows notepad to make the file. Click to expand... You should NOT use this F**** app. Use Notepad++ Notepad is FAR FAR FAR FAR AWAY from being s good Software (It is not even a akceptable one)!! Notepad is a NO GOOOOO for any programmer Upvote 0
Branko Milosevic said: Using stock windows notepad to make the file. Click to expand... You should NOT use this F**** app. Use Notepad++ Notepad is FAR FAR FAR FAR AWAY from being s good Software (It is not even a akceptable one)!! Notepad is a NO GOOOOO for any programmer
B Branko Milosevic Active Member Licensed User May 2, 2017 #18 eps said: What type of text file is the CSV file? Ideally check it using Notepad++ or similar - to ensure that it is a valid text file. I think @Erel posted a solution for parsing a csv file before, here : https://www.b4x.com/android/forum/t...ljava-lang-string-2bc5dbc0.29655/#post-172099 It may well be that you're parsing the file incorrectly, try the code above and debug it - the log will show you if the lines are being processed as you expect. Click to expand... I had an extra line with a CRLF at the end of the CSV file. So will try later today if that fixes it. Upvote 0
eps said: What type of text file is the CSV file? Ideally check it using Notepad++ or similar - to ensure that it is a valid text file. I think @Erel posted a solution for parsing a csv file before, here : https://www.b4x.com/android/forum/t...ljava-lang-string-2bc5dbc0.29655/#post-172099 It may well be that you're parsing the file incorrectly, try the code above and debug it - the log will show you if the lines are being processed as you expect. Click to expand... I had an extra line with a CRLF at the end of the CSV file. So will try later today if that fixes it.
jimmyF Active Member Licensed User Longtime User May 2, 2017 #19 Have you tried double quotation marks around your string values when you generate the csv file? Some csv parsers require them. just sayin'... Upvote 0
Have you tried double quotation marks around your string values when you generate the csv file? Some csv parsers require them. just sayin'...
B Branko Milosevic Active Member Licensed User May 2, 2017 #20 jimmyF said: Have you tried double quotation marks around your string values when you generate the csv file? Some csv parsers require them. just sayin'... Click to expand... It's manually edited file. I don't have any quotation marks, string array I am assuming will automatically convert all values to strings. Upvote 0
jimmyF said: Have you tried double quotation marks around your string values when you generate the csv file? Some csv parsers require them. just sayin'... Click to expand... It's manually edited file. I don't have any quotation marks, string array I am assuming will automatically convert all values to strings.