0

If we have a file containing csv then we can read it using 0: say, we have a file x.csv on the disk then converting it to a table is easy as below

("SFJ";enlist",")0:`:/x.csv

But, how can we covert a csv string to table?
string:

"sym,px,vol
GG,10.2,100
AA,11.2,1000"

Expected output: table

sym px  vol
"GG"    10.2    100
"AA"    11.2    1000
1
  • Convert a CSV string to a table in CSV? Commented Feb 5, 2020 at 6:01

2 Answers 2

3

A string can be passed in using 0: instead of a file handle, and the table will be created as normal:

q)s:("sym,px,vol";"GG,10.2,100";"AA,11.2,1000")
q)s
"sym,px,vol"
"GG,10.2,100"
"AA,11.2,1000"
q)("SFJ";enlist",")0:s
sym px   vol
-------------
GG  10.2 100
AA  11.2 1000
Sign up to request clarification or add additional context in comments.

Comments

2

If you needed to programmatically get to Eliot's s from one big string csv there are a few options depending on the format of the csv string.

// \n delimited 
s:` vs "sym,px,vol\nGG,10.2,100\nAA,11.2,1000"

// if you know the row and col count. 
s:3 3#"," vs "sym,px,vol,GG,10.2,100,AA,11.2,1000"

// if you just know the col count
s:"sym,px,vol,GG,10.2,100,AA,11.2,1000"
f:{[str;noCol]
    str:"," vs str;   
    noRow:`long$(count str)%noCol;
    (noRow, noCol)#str
}
f[s;3] 

All three output this ("sym,px,vol";"GG,10.2,100";"AA,11.2,1000")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.