kdb +:将表保存到csv文件中 [英] kdb+: Save table into a csv file

查看:212
本文介绍了kdb +:将表保存到csv文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下表"dates",它有一个带符号的sym列和一个带字符串列表的d列,并希望将其保存到常规CSV文件中.找不到一种好的方法.有什么建议吗?

I have the below table "dates" , it has a sym column with symbols and a d column with list of strings and would like to save it into a regular CSV file. Couldn't find a good way to do it. Any suggestions?

q)dates
sym  d
----------------------------------------------------------------------------
6AH0 "1970.03.16" "1980.03.17" "1990.03.19" "2010.03.15" 
6AH6 "1976.03.15" "1986.03.17" "1996.03.18" "2016.03.14" 
6AH7 "1977.03.14" "1987.03.16" "1997.03.17" "2017.03.13" 
6AH8 "1978.03.13" "1988.03.14" "1998.03.16" "2018.03.19" 
6AH9 "1979.03.19" "1989.03.13" "1999.03.15" "2019.03.18" 

当我尝试定期保存时,会发生以下错误:

When I try to do the regular save the below error happens:

q)save `:dates.csv
k){$[t&77>t:@y;$y;x;-14!'y;y]}
'type
q))

推荐答案

Kdb +中的内部table-> csv转换功能无法处理列中的嵌套列表.表中的d列是字符列表的列表.但是,转换功能只能处理一个简单的嵌套列(深度为1).

The internal table->csv conversion function within Kdb+ is not able to handle nested lists in columns. The d column in your table is a list of list of chars. However, the conversion function is able to handle a simply nested column (depth of 1).

因此,您可以将d列转换为字符列表,然后使用内部函数保存为CSV:

Therefore, you can convert the d column to a list of chars and then save to CSV using the internal function:

/ generate a table of dummy data
q)show dates:flip `sym`d!(`6AH0`6AH6`6AH7;string (3;0N)#12?.z.d)
    sym  d
    --------------------------------------------------------
    6AH0 "2008.02.04" "2015.01.02" "2003.07.05" "2005.02.25"
    6AH6 "2012.10.25" "2008.08.28" "2017.01.25" "2007.12.27"
    6AH7 "2004.02.01" "2005.06.06" "2013.02.11" "2010.12.20"

/ convert 'd' column to simple list - the (" " sv') is the conversion func here
q)@[`dates;`d;" " sv']
    `dates

/ review what was done
q)show dates
    sym  d
    --------------------------------------------------
    6AH0 "2008.02.04 2015.01.02 2003.07.05 2005.02.25"
    6AH6 "2012.10.25 2008.08.28 2017.01.25 2007.12.27"
    6AH7 "2004.02.01 2005.06.06 2013.02.11 2010.12.20"

/ save to csv
q)save `:dates.csv
    `:dates.csv

/ review saved csv 
q)\cat dates.csv
    "sym,d"
    "6AH0,2008.02.04 2015.01.02 2003.07.05 2005.02.25"
    "6AH6,2012.10.25 2008.08.28 2017.01.25 2007.12.27"
    "6AH7,2004.02.01 2005.06.06 2013.02.11 2010.12.20"

这篇关于kdb +:将表保存到csv文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆