Search code examples
kdb

KDB:Trying to read multiple csv files at a location


I am trying to run below code to read all csv files available at location C:/q/BitCoin/Input.Getting an error and dont know what the solution is?csv files are standard ones with three fields.

raze{[x] 
inputdir:`:C:/q/BitCoin/Input;
filelist1:key inputdir;
filelist2:` sv' inputdir,'filelist1;
filelist3:string filelist2;
r:flip`Time`Qty`Price!("ZFF";",")0:x; 
select from r
} each `$filelist3

Hard coding the file names and running below code works but I don't want to hard code

raze {[x] 
r:flip`Time`Qty`Price!("ZFF";",")0:x; 
select from r 
} each (`$"C:/q/BitCoin/Input/bitbayPLN.csv";`$"C:/q/BitCoin/Input/anxhkAUD.csv")

Getting below error

An error occurred during execution of the query. The server sent the response: filelist3

Can someone help with issue?


Solution

  • The reason that you are receiving the error 'filelist3 is because filelist3 is defined in the lambda and outside of the lambda it is not recognised or defined. There are various ways to overcome this as outlined below.

    Firstly you can essentially take all of the defined work done on the inside of the lambda and put it on the right side of the each.

    raze{[x] r:flip`Time`Qty`Price!("ZFF";",")0:x; select from r
      } each `$(string (` sv' `:C:/q/BitCoin/Input,'(key `:C:/q/BitCoin/Input)))
    

    Or if you wanted to you could create a function which will generate filelist3 for you and use that on the right hand side of the each also.

    f:{[inputdir] filelist1:key inputdir; filelist2:` sv' inputdir,'filelist1; filelist3:string filelist2; filelist3}
    
    raze{[x] r:flip`Time`Qty`Price!("ZFF";",")0:x; select from r
      } each `$f[`:C:/q/BitCoin/Input]
    

    I hope this helps.

    Many thanks,

    Joel