How to export each table in csv to kdb + database?

Let's assume my kdb + database has multiple tables. How can I export all tables to csv files where the name of each csv is the same as the table name?

+3


source to share


2 answers


There may be several ways to approach this, one solution could be:

 q)t1:([]a:1 2 3;b:1 2 3)
 q)t2:([]a:1 2 3;b:1 2 3;c:1 2 3)
 q){save `$(string x),".csv"} each tables[]
   `:t1.csv`:t2.csv

      

ref: http://code.kx.com/q/ref/filewords/#save



If you want to specify the directory of the saved file, you can leverage the function like this:

q){[dir;tSym] save ` sv dir,(`$ raze string tSym,`.csv)}[`:C:/Users/dhughes1/Documents;] each tables[]
  `:C:/Users/dhughes1/Documents/t1.csv`:C:/Users/dhughes1/Documents/t2.csv

      

+7


source


An alternative method save

is to use it 0:

in prepare , specifying the "," separator:

q)tab:([]a:1 2 3;b:`a`b`c)
q)show t:","0:tab
"a,b"
"1,a"
"2,b"
"3,c"

      

And again save the text:



q)`:tab 0: t
`:tab

      

The advantage of this method is that the delimiter can be specified before saving to disk.

0


source







All Articles