Is there a way to use strings as separators in writetable() - Julia

别来无恙 提交于 2019-12-10 15:55:53

问题


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?

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?


回答1:


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)

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

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



来源:https://stackoverflow.com/questions/38061746/is-there-a-way-to-use-strings-as-separators-in-writetable-julia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!