Search code examples
export-to-csvkdb

kdb+: Save table with a column with a list of float into a csv file


I have a table "floats" with two columns: sym and prices. sym elements are strings and prices elements are list of floats.

q)LF:((3.0;1.0;2.0);(5.0;7.0;4.0);(2.0;8.0;9.0))
q)show floats:flip `sym`prices!(`6AH0`6AH6`6AH7;LF)
sym  prices
-----------
6AH0 3 1 2
6AH6 5 7 4
6AH7 2 8 9

I want to export the table "floats" on a csv file but I get this error:

q)save `:floats.csv
'type
[0]  save `:floats.csv

I followed this post kdb+: Save table into a csv file which solves the problem if the column is a list of string. Unfortunately when I try to convert the "prices" column to a list of chars and then save to CSV using the internal function, the procedure returns errors:

q))@[`floats;`prices;" " sv']
'type
[7]  @[`floats;`prices;" " sv']
     ^

q))@[`floats;`prices;string]
'noamend: `. `floats
[10] @[`floats;`prices;string]
     ^
q))@[`floats;string `prices;" " sv']
'noamend: `. `floats
[10] @[`floats;string `prices;" " sv']
     ^

Please help me in converting the "prices" column to a list of chars and then save to CSV using the internal function or provide valid alternatives to export the table on a text file.


Solution

  • First, you need to convert float to string then use sv with adverb each right denoted by /: .

    floats: update " " sv/: string each prices from floats