Search code examples
kdb

Set header when creating table from CSV


I have a csv "pf.csv":

Jules,Winnfield  
Vincent,Vega  
Mia,Wallace  
Marsellus,Wallace  

And would like to specify a list of symbols which become the header when I read the csv. Normally I would load the csv like so:

("SS";enlist ",") 0: `$"pf.csv"

but that actually sets the first row as the keys in the flipped dictionary (i.e. the header in the table)
In the documentation for 0: I read

Optionally, 0: can take a three-item list as its second argument, containing the file handle, an offset at which to begin reading, and a length to read.

But that's inconvenient as the offset has to be given in number of characters and not in lines.


Solution

  • The way to go about this is to specify the column names before the bit you use to load the csv.

    flip`fname`surname!("SS";",")0:`:pf.csv
    

    You will also have to drop enlist because you do not have any column headers in your csv.

    Another option would be to name the columns inside your *.csv file and then you can simply use enlist in your query to specify that the first row contains the column names.

    Some more details here:

    http://code.kx.com/q4m3/11_IO/#1152-variable-length-records https://code.kx.com/wiki/Reference/ZeroColon#Load_Delimited_Records_.28Read_CSV.29