有没有办法在writetable() - Julia中使用字符串作为分隔符 [英] Is there a way to use strings as separators in writetable() - Julia

查看:196
本文介绍了有没有办法在writetable() - Julia中使用字符串作为分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用writetable()将数据帧写入文件时,我希望能够使分隔符成为空格,然后是逗号(即,作为分隔符)。我知道writetable()只能有一个char作为分隔符参数的选项。是否有可能的解决方法能够使用字符串作为分隔符?

When using writetable() to write a Data Frame to a file, I would like to be able to have the delimiter be a space then a comma (i.e. " ," as the delimiter). I know writetable() only has the option to have a single char as the separator argument. Is there a possible workaround to be able to have a string as the delimiter?

或者,是否可以在数据帧中的每个单个数据点之后添加一个空格然后将其正常输出到.csv文件,因此在文件中基本上具有,分隔符?

Or, is it possible to simply add a space after every single data point in the data frame and then output it as normal to a .csv file, therefore essentially having the " ," delimiter in the file?

推荐答案

你如果将DataFrame转换为数组,可以使用 writedlm 函数:

You can do this with the writedlm function if you convert your DataFrame to an Array:

using DataFrames
A = DataFrame(rand(3,3));
B = convert(Array, A);
writedlm("/path/to/file.txt", B,  ", ")

要包含一个标题,您可以使用以下内容:

To include a header you can use something like this:

f = open("/path/to/file.txt", "w")
writedlm(f, names(A)', ", ")
writedlm(f, B,  ", ")
close(f)

注1:不要忘记' code>名字(A)'

Note1: Don't forget the ' transpose operation on names(A)'

Note2:很快,我相信 writedlm 将直接有一个 header_string 选项。请参阅此处

Note2: soonish, I believe that writedlm will directly have a header_string option. See here.

这篇关于有没有办法在writetable() - Julia中使用字符串作为分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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