Reputation: 295
I am importing a csv file to a table, but some columns have spaces in their names. Is there anyway around this or do I need to rename the columns before importing?
Upvotes: 2
Views: 4860
Reputation: 4213
.Q.id
will remove any non-alphanumeric characters, as well as it will rename any columns which interfere with q namespace.
Example:
q)flip(`$("a b c";"d e f";"name©"))!3#()
a b c d e f name©
------------------
With .Q.id
:
q).Q.id flip(`$("a b c";"d e f";"name©"))!3#()
abc def name
------------
More information can be found at code.kx.com.
Upvotes: 1
Reputation: 1919
If you want to clean up your columns names generically, the below cleancols
function could be useful
rmbad:{`$string[x] inter\: .Q.an} //remove bad characters
//make sure first elem is a char
inichar:{`$@[s; where in[ ;.Q.n] first each s:string x;"c",]}
//rename duplicates
dupes:{@[x;g n;:;`$string[n],/:'string til each gc n:where 1<gc:count each g:group x]}
cleancols:dupes inichar rmbad cols@ //clean column names
cleancols[x] xcol x:flip (`$("bad*";"ba;d*"))!5 cut til 10
cleancols[x] xcol x:flip (`$("ok1";"1&* (ba;d*"))!5 cut til 10
Upvotes: 0
Reputation: 955
To remove spaces from column names in table t:
t:xcol[`$ssr[;" ";""]each string cols t;t]
Upvotes: 1