Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kdb+: Save table into a csv file

I have the below table "dates" , it has a sym column with symbols and a d column with list of strings and would like to save it into a regular CSV file. Couldn't find a good way to do it. Any suggestions?

q)dates
sym  d
----------------------------------------------------------------------------
6AH0 "1970.03.16" "1980.03.17" "1990.03.19" "2010.03.15" 
6AH6 "1976.03.15" "1986.03.17" "1996.03.18" "2016.03.14" 
6AH7 "1977.03.14" "1987.03.16" "1997.03.17" "2017.03.13" 
6AH8 "1978.03.13" "1988.03.14" "1998.03.16" "2018.03.19" 
6AH9 "1979.03.19" "1989.03.13" "1999.03.15" "2019.03.18" 

When I try to do the regular save the below error happens:

q)save `:dates.csv
k){$[t&77>t:@y;$y;x;-14!'y;y]}
'type
q))
like image 594
Alim Hasanov Avatar asked Oct 16 '25 19:10

Alim Hasanov


1 Answers

The internal table->csv conversion function within Kdb+ is not able to handle nested lists in columns. The d column in your table is a list of list of chars. However, the conversion function is able to handle a simply nested column (depth of 1).

Therefore, you can convert the d column to a list of chars and then save to CSV using the internal function:

/ generate a table of dummy data
q)show dates:flip `sym`d!(`6AH0`6AH6`6AH7;string (3;0N)#12?.z.d)
    sym  d
    --------------------------------------------------------
    6AH0 "2008.02.04" "2015.01.02" "2003.07.05" "2005.02.25"
    6AH6 "2012.10.25" "2008.08.28" "2017.01.25" "2007.12.27"
    6AH7 "2004.02.01" "2005.06.06" "2013.02.11" "2010.12.20"

/ convert 'd' column to simple list - the (" " sv') is the conversion func here
q)@[`dates;`d;" " sv']
    `dates

/ review what was done
q)show dates
    sym  d
    --------------------------------------------------
    6AH0 "2008.02.04 2015.01.02 2003.07.05 2005.02.25"
    6AH6 "2012.10.25 2008.08.28 2017.01.25 2007.12.27"
    6AH7 "2004.02.01 2005.06.06 2013.02.11 2010.12.20"

/ save to csv
q)save `:dates.csv
    `:dates.csv

/ review saved csv 
q)\cat dates.csv
    "sym,d"
    "6AH0,2008.02.04 2015.01.02 2003.07.05 2005.02.25"
    "6AH6,2012.10.25 2008.08.28 2017.01.25 2007.12.27"
    "6AH7,2004.02.01 2005.06.06 2013.02.11 2010.12.20"
like image 89
MdSalih Avatar answered Oct 19 '25 06:10

MdSalih



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!