Writing
CSV.write
— FunctionCSV.write(file, table; kwargs...) => file
table |> CSV.write(file; kwargs...) => file
Write a Tables.jl interface input to a csv file, given as an IO
argument or String
/FilePaths.jl type representing the file name to write to. Alternatively, CSV.RowWriter
creates a row iterator, producing a csv-formatted string for each row in an input table.
Supported keyword arguments include:
bufsize::Int=2^22
: The length of the buffer to use when writing each csv-formatted row; default 4MB; if a row is larger than thebufsize
an error is throwndelim::Union{Char, String}=','
: a character or string to print out as the file's delimiterquotechar::Char='"'
: ascii character to use for quoting text fields that may contain delimiters or newlinesopenquotechar::Char
: instead ofquotechar
, useopenquotechar
andclosequotechar
to support different starting and ending quote charactersescapechar::Char='"'
: ascii character used to escape quote characters in a text fieldmissingstring::String=""
: string to print formissing
valuesdateformat=Dates.default_format(T)
: the date format string to use for printing outDate
&DateTime
columnsappend=false
: whether to append writing to an existing file/IO, iftrue
, it will not write column names by defaultcompress=false
: compress the written output using standard gzip compression (provided by the CodecZlib.jl package); note that a compression stream can always be provided as the first "file" argument to support other forms of compression; passingcompress=true
is just for convenience to avoid needing to manually setup a GzipCompressorStreamwriteheader=!append
: whether to write an initial row of delimited column names, not written by default if appendingheader
: pass a list of column names (Symbols or Strings) to use instead of the column names of the input tablenewline='\n'
: character or string to use to separate rows (lines in the csv file)quotestrings=false
: whether to force all strings to be quoted or notdecimal='.'
: character to use as the decimal point when writing floating point numberstransform=(col,val)->val
: a function that is applied to every cell e.g. we can transform allnothing
values tomissing
using(col, val) -> something(val, missing)
bom=false
: whether to write a UTF-8 BOM header (0xEF 0xBB 0xBF) or notpartition::Bool=false
: by passingtrue
, thetable
argument is expected to implementTables.partitions
and thefile
argument can either be an indexable collection ofIO
, fileString
s, or a single fileString
that will have an index appended to the name
Examples
using CSV, Tables, DataFrames
# write out a DataFrame to csv file
df = DataFrame(rand(10, 10), :auto)
CSV.write("data.csv", df)
# write a matrix to an in-memory IOBuffer
io = IOBuffer()
mat = rand(10, 10)
CSV.write(io, Tables.table(mat))
CSV.RowWriter
— TypeCSV.RowWriter(table; kwargs...)
Creates an iterator that produces csv-formatted strings for each row in the input table.
Supported keyword arguments include:
bufsize::Int=2^22
: The length of the buffer to use when writing each csv-formatted row; default 4MB; if a row is larger than thebufsize
an error is throwndelim::Union{Char, String}=','
: a character or string to print out as the file's delimiterquotechar::Char='"'
: ascii character to use for quoting text fields that may contain delimiters or newlinesopenquotechar::Char
: instead ofquotechar
, useopenquotechar
andclosequotechar
to support different starting and ending quote charactersescapechar::Char='"'
: ascii character used to escape quote characters in a text fieldmissingstring::String=""
: string to print formissing
valuesdateformat=Dates.default_format(T)
: the date format string to use for printing outDate
&DateTime
columnsheader
: pass a list of column names (Symbols or Strings) to use instead of the column names of the input tablenewline='\n'
: character or string to use to separate rows (lines in the csv file)quotestrings=false
: whether to force all strings to be quoted or notdecimal='.'
: character to use as the decimal point when writing floating point numberstransform=(col,val)->val
: a function that is applied to every cell e.g. we can transform allnothing
values tomissing
using(col, val) -> something(val, missing)
bom=false
: whether to write a UTF-8 BOM header (0xEF 0xBB 0xBF) or not