cancel
Showing results for 
Search instead for 
Did you mean: 

Exporting CSV file

Aakash
New Contributor II

I want to export a CSV from kdb.
i want to add a tab with each column. How can i achieve that?

 

data: enlist"\n"sv ","0:rows

 

 

1 ACCEPTED SOLUTION

Note, this was just a quick attempt:

q)testfile:flip {(1#x),"\t",'/:1_x} csv vs' csv 0: t
save `:testfile.csv

davidcrossey_0-1690184962301.png

View solution in original post

5 REPLIES 5

davidcrossey
Moderator Moderator
Moderator

Hi @Aakash,

Your question isn't very clear. Do you want to export a table to a csv, or a table with column headers that include tabs?

Have a look at Handling CSVs in kdb+ | A tour of the q programming language | Documentation for kdb+ and the q prog...

Aakash
New Contributor II

Hi @davidcrossey 
I want to export a table to a csv, where each new rows with type string needs to have tabs under each column.

I'm not following your request. I read it as follows, which I don't think is what you mean?

 

q)t:([]c1:`a`b`c;c2:1 2 3)

q){cc:count cols t;x:csv 0: x;(1#x),enlist[(csv sv cc# enlist enlist "\t")],1_x} t
"c1,c2"
"\t,\t"
"a,1"
"b,2"
"c,3"

q)(count[cols t]#"*";enlist csv) 0: {cc:count cols t;x:csv 0: x;(1#x),enlist[(csv sv cc# enlist enlist "\t")],1_x} t
c1    c2
-----------
,"\t" ,"\t"
,"a"  ,"1"
,"b"  ,"2"
,"c"  ,"3"

 

If you prepare you csv data and read from disk, you'll recreate a table without needing to add tabs:

 

q)csv 0: t
"c1,c2"
"a,1"
"b,2"
"c,3"

q)(count[cols t]#"*";enlist csv) 0: csv 0: t
c1   c2
---------
,"a" ,"1"
,"b" ,"2"
,"c" ,"3"

 


Can you provide a before and after example of what you mean?

Aakash
New Contributor II

Before
BeforeBefore

After

AfterAfter

Note, this was just a quick attempt:

q)testfile:flip {(1#x),"\t",'/:1_x} csv vs' csv 0: t
save `:testfile.csv

davidcrossey_0-1690184962301.png