在 q/kdb 中读取带有注释和空行的 csv 文件
Read a csv file with comments and empty lines in q/kdb
我正在尝试读取一个包含注释和空行的 csv 文件,我需要从中获取非空行或非注释行。
文件如下所示:
干 运行:
的测试文件
#This is a comment
# This is a comment with, comma
# This,is,a,comment with exact number of commas as valid lines
h1,h2,h3,h4
a,b,c,d
e,f,g,h
i,j,k,l
m,n,o,p
预期输出:
h1 h2 h3 h4
-----------
a b c d
e f g h
i j k l
m n o p
尝试失败:
q)("SSSS";enlist ",")0: ssr[;;]each read0 `:test.csv // tried various options with ssr but since '*' wildcard gives error with ssr so not sure of how to use regex here
这提供了所需的结果:
q)("SSSS";enlist",")0:t where not""~/:t:5_read0`:test.csv
h1 h2 h3 h4
-----------
a b c d
e f g h
i j k l
m n o p
要忽略任意数量的评论,您可以使用:
q)("SSSS";enlist",")0:t where not any each(" ";"#")~\:/:first each t:read0`:test.csv
h1 h2 h3 h4
-----------
a b c d
e f g h
i j k l
m n o p
我正在尝试读取一个包含注释和空行的 csv 文件,我需要从中获取非空行或非注释行。
文件如下所示:
干 运行:
#This is a comment
# This is a comment with, comma
# This,is,a,comment with exact number of commas as valid lines
h1,h2,h3,h4
a,b,c,d
e,f,g,h
i,j,k,l
m,n,o,p
预期输出:
h1 h2 h3 h4
-----------
a b c d
e f g h
i j k l
m n o p
尝试失败:
q)("SSSS";enlist ",")0: ssr[;;]each read0 `:test.csv // tried various options with ssr but since '*' wildcard gives error with ssr so not sure of how to use regex here
这提供了所需的结果:
q)("SSSS";enlist",")0:t where not""~/:t:5_read0`:test.csv
h1 h2 h3 h4
-----------
a b c d
e f g h
i j k l
m n o p
要忽略任意数量的评论,您可以使用:
q)("SSSS";enlist",")0:t where not any each(" ";"#")~\:/:first each t:read0`:test.csv
h1 h2 h3 h4
-----------
a b c d
e f g h
i j k l
m n o p