CSV Files

All,

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

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.

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

function setup()  
    tab={1,2,3,4,5}
    a=json.encode(tab)
    print(a)
    
    str='["one","two","three","four","five"]'
    tab=json.decode(str)    
    print(table.concat(tab,"\
"))      
end

@Bri_G the Programming in Lua guide has a piece on parsing CSV in this chapter: https://www.lua.org/pil/20.4.html

@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 file.io 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.

@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)

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

@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. ?

@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)

@Simeon - what you quoted above is exactly what I did - but neither path nor CSV were declared local.

@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.

@Simeon @dave1707 - struggling with parsing a csv file at moment. Seems like io.file() io.open 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.

@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,qw.er.ty.ui.op,"

    split()

end

function split()
    tab={}
    for value,_ in string.gmatch(str,"(.-)(,)") do
        --print(value)
        table.insert(tab,value)
    end
    print(table.concat(tab,"\
"))
end

@dave1707 - thanks for that - I got too fixated on trying to get readText() and io.file, io.read() 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.