Android Question reading comma separated values text file

Branko Milosevic

Active Member
Licensed User
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.
 

freedom2000

Well-Known Member
Licensed User
Longtime User
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.", " ")
B4X:
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

Didier9

Well-Known Member
Licensed User
Longtime User
There is more than one way to do it :)
I use RegEx.Split as below:
B4X:
Dim s as String
s = "This,is,a,test"
Dim item() As String = Regex.Split("\,", s)
After this you find:
B4X:
item(0) = "This"
item(1) = "is"
...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

Branko Milosevic

Active Member
Licensed User
You shoud show us your code and provide a csv file.
Without seeing what exactly you have done it's impossible to help!

Here is the code:
B4X:
'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:
B4X:
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

Expert
Licensed User
Longtime User
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.
 
  • Like
Reactions: eps
Upvote 0

eps

Expert
Licensed User
Longtime User
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

Branko Milosevic

Active Member
Licensed User
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.
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

DonManfred

Expert
Licensed User
Longtime User
Using stock windows notepad to make the file.
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

Active Member
Licensed User
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.
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

jimmyF

Active Member
Licensed User
Longtime User
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

Branko Milosevic

Active Member
Licensed User
Have you tried double quotation marks around your string values when you generate the csv file?
Some csv parsers require them.
just sayin'...
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
Top