سؤال

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?

هل كانت مفيدة؟

المحلول

To remove spaces from column names in table t:

t:xcol[`$ssr[;" ";""]each string cols t;t]

نصائح أخرى

.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.

Once you've read the CSV data into a table, you can rename the columns using xcol.

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top