Search code examples
csvdataframejuliadelimiterseparator

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


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?


Solution

  • 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.