Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

CSV Files

in General Posts: 2,348


Anyone ever tried to load a CSV file into Codea? Would like to try it but unsure how to start.



  • dave1707dave1707 Mod
    Posts: 9,415

    A csv file is just a comma-separated-value file. json.encode takes a table and creates a csv string and json.decode reads it and creates a table. So depending on your file, you can create a table of values and go from there. What are you actually trying to do and what’s your file like.

  • dave1707dave1707 Mod
    edited March 8 Posts: 9,415

    @Bri_G Heres an example of json csv string/file explained above.

    function setup()  
  • SimeonSimeon Admin Mod
    Posts: 5,694

    @Bri_G the Programming in Lua guide has a piece on parsing CSV in this chapter:

  • Posts: 2,348

    @dave1707 @Simeon - thanks for the feedback. I'll digest the book extract. But my first problem is to find the syntax for opening a text/CSV file with in Lua.

    I'm currently trying a backup of pasting the data into a tab and converting it into a table - long job.

    Ideally I'd like to convert a Python or C conversion of the generating code to Lua, that's slow but I'm hopeful I'll get there in the end.

    Also, noted that a text file, say info.txt will only read as info_txt in the asset viewer within Codea. Like the obj loading I have to use object_obj.obj as file name to get them to load from Dropbox.

  • SimeonSimeon Admin Mod
    edited March 9 Posts: 5,694

    @Bri_G you could use readText to read the text file from your Documents location or elsewhere.

    You can create a new text document in Codea by:

    1. Open the left pane on the home screen
    2. Select "Assets"
    3. Select "Documents"
    4. Tap "+ New text file"
    5. Enter file name (e.g. "MyFile.csv")
    6. Tap the little pencil icon in the top corner of the new file
    7. Paste your CSV data into the file

    In Codea you can do local csv = readText(asset.documents.MyFile)

  • Posts: 2,348

    @Simeon - thanks for the feedback, will get on with that when my pad has recharged. Thanks again.

  • edited March 9 Posts: 2,348

    @Simeon - now loading the csv file, just need to parse the strings now. Thanks.

    One point to note, threw up an error when I assigned the path to a variable - asset strings deprecated. No error when I placed the asset path in the readText() brackets. Is this consistent with asset path syntax in other situations - sprite/model etc. ?

  • SimeonSimeon Admin Mod
    Posts: 5,694

    @Bri_G can you give me an example of what you mean about getting an error when assigning the path to a variable?

    Something like this shouldn't give an error:

    local path = asset.documents.MyFile
    local csv = readText(path)
  • Posts: 2,348
    @Simeon - what you quoted above is exactly what I did - but neither path nor CSV were declared local.
  • Posts: 2,348

    @Simeon - ignore the error on path usage in readText(). The error seemed to embed in the error report. Checked out and edited the csv file and now works Ok using path in the readText() function. Must have been the errors in the csv file itself.

  • Posts: 2,348

    @Simeon @dave1707 - struggling with parsing a csv file at moment. Seems like io.file() etc don’t accept a path variable or the current Codea asset format for path.

    I can load a 256 line csv file with readText() but I am trying to the break down each of the 256 lines (each containing 10 csv items) into a table/tables so that I can interrogate it.

    Can you give me some pointers or a small example. I can load the complete csv string and print out a single string but struggle from there on.

  • dave1707dave1707 Mod
    edited March 11 Posts: 9,415

    @Bri_G Heres an example that creates a table and prints each value that ends in a comma. I use a simple string that contains the comma separated values. You should be able to create a string using readtext.

    I also tried a simple text file and used readtext to create the string just to make sure it would work.

    function setup()
        str="1,22,3:3:3,44.44,55+5+55,6-66-66-6,one,two,three,four,five,one two three four five six,,"
    function split()
        for value,_ in string.gmatch(str,"(.-)(,)") do
  • Posts: 2,348

    @dave1707 - thanks for that - I got too fixated on trying to get readText() and io.file, and io.line() trying to get them to work and completely forgot the string functions . A Homer moment doooooh !!!
    Thanks again - hopefully posting on this soon.

Sign In or Register to comment.