【问题标题】:Read a csv file with comments and empty lines in q/kdb在 q/kdb 中读取带有注释和空行的 csv 文件
【发布时间】:2021-04-16 15:09:00
【问题描述】:

我正在尝试读取包含 cmets 和空行的 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

【问题讨论】:

  • 你在什么系统上运行?如果您在基于 unix 的系统上,您应该可以访问 sed 实用程序,该实用程序可用于预处理文件。 sed -i -e '/^$/d' -e '/^#/d' test.csv 这将首先删除空行,然后删除 cmets。然后可以使用来自 q 进程的("SSSS";enlist ",")0:`:test.csv 读取该文件
  • 我需要在 q/kdb 中直接读取它作为 q 脚本的一部分。

标签: kdb


【解决方案1】:

这提供了所需的结果:

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

要忽略可以使用的任意数量的 cmets:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多