【发布时间】: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