Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a dictionary from contents of a csv file in kdb?

Tags:

q-lang

kdb+

I have a csv file with contents like below

source,address,table,tableName,sym,symSet
source_one,addr1:port1:id1:pass1,table_one,tableName1,syms_one,SYM1 SYM2 SYM3
source_two,addr2:port2:id2:pass2,table_two,tableName2,syms_two,SYM21 SYM22 SYM23

My code to load a csv into a table is as below

table:("******";enlist ",") 0: `sourceFileName.csv

I want to create a dictionary out of contents of 'table' in the below format

source_one|addr1:port1:id1:pass1
table_one|tableName1
syms_one|SYM1 SYM2 SYM3
source_two|addr2:port2:id2:pass2
table_two|tableName2
syms_two|SYM21 SYM22 SYM23

How do I achieve this?

Thanks!

like image 814
CleanSock Avatar asked Nov 19 '25 16:11

CleanSock


1 Answers

You can also use 0: to directly Parse Key-Value Pairs however it would require a change to the way your text file is stored.

Need to drop the first line and add comma on the end of each line:

$ cat test.txt
source_one=addr1:port1:id1:pass1,table_one=tableName1,syms_one=SYM1 SYM2 SYM3,
source_two=addr2:port2:id2:pass2,table_two=tableName2,syms_two=SYM21 SYM22 SYM23,

If its easy to change the load then becomes one line:

q)(!). "S=,"0: raze  read0 `:test.txt
source_one| "addr1:port1:id1:pass1"
table_one | "tableName1"
syms_one  | "SYM1 SYM2 SYM3"
source_two| "addr2:port2:id2:pass2"
table_two | "tableName2"
syms_two  | "SYM21 SYM22 SYM23"

This has the advantage over loading to a table if the data is irregular, .e.g not ever line has source and table and syms. If they did why not just have those as column names in a table?

like image 137
emc211 Avatar answered Nov 22 '25 05:11

emc211



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!