KX Community

Find answers, ask questions, and connect with our KX Community around the world.
KX Community Guidelines

Home Forums kdb+ Exporting CSV file

  • Exporting CSV file

    Posted by aakash on July 24, 2023 at 12:00 am

    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

     

     

    aakash replied 8 months, 2 weeks ago 2 Members · 4 Replies
  • 4 Replies
  • davidcrossey

    Member
    July 24, 2023 at 12:00 am

    Note, this was just a quick attempt:

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

     

  • davidcrossey

    Member
    July 24, 2023 at 12:00 am

    Hi ,

    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 programming language – Kdb+ and q documentation (kx.com)

  • aakash

    Member
    July 24, 2023 at 12:00 am

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

  • davidcrossey

    Member
    July 24, 2023 at 12:00 am

    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?

Log in to reply.