Skip to content

Convenience functions for writing CSV #59

@abelsiqueira

Description

@abelsiqueira

Description

Create a function that saves a table as CSV, possibly adding the prefixes row.

Taken from TulipaClustering:

"""
    write_csv_with_prefixes(file_path, df; prefixes)

Writes the dataframe `df` into a csv file at `file_path`. If `prefixes` are
provided, they are written above the column names. For example, these prefixes
can contain metadata describing the columns.
"""
function write_csv_with_prefixes(file_path, df; prefixes = nothing, csvargs...)
  if isnothing(prefixes) || length(prefixes) == 0
    # If there are no prefixes, just write the data frame into the file
    CSV.write(file_path, df; strict = true, csvargs...)
  else
    # Convert the prefixes to a one-row table for `CSV.write` to use
    prefixes = reshape(prefixes, (1, length(prefixes))) |> Tables.table
    # First we write the prefixes, then we append the data drom the dataframe
    CSV.write(file_path, prefixes; header = false, strict = true, csvargs...)
    CSV.write(file_path, df; header = true, append = true, strict = true, csvargs...)
  end
  return nothing
end

Motivation

No response

Target audience

No response

Can you help

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions