Read and write CSV with Julia CSV.jl

A practical review of Julia's CSV.jl: how it reads and writes CSV and TSV, what the conversion changes in your data, and when plain text or another format fits better.

CSV.jl is the standard Julia package for reading and writing delimited text such as CSV and TSV. It parses a text file into typed columns and writes tabular data back out as text. This review covers what that round trip changes in your data, where the output stays portable, and when a different approach makes more sense. It is based on the package's documented behavior and general properties of delimited text, not on benchmarks or hands-on tests. Where a claim depends on a specific environment, it is marked as something to verify yourself.

Is CSV.jl suitable for your use?

If you already work in Julia and need to move tabular data in or out as delimited text, CSV.jl is the sensible default. Its main benefit is that it turns text into typed, column-oriented data with little setup. It plugs into the Tables.jl interface, so the result can go straight into a DataFrame or any other compatible table type.

The key tradeoff is that reading a file means interpreting it. CSV.jl guesses column types, decides what counts as missing, and may detect the delimiter for you. Those guesses are usually convenient. They can also quietly change values that only look numeric or date-like, such as identifiers with leading zeros.

Working with the file as plain text is the better choice in these cases:

  • You only need to inspect, search or lightly edit the file, not compute on it.
  • The exact original characters must be preserved byte for byte, for example in audit or archival work.
  • The file is not consistently tabular, with ragged rows or several record types mixed together.
  • The output is meant for people to read rather than for software to process.

Getting started

Install the package with Julia's package manager. Then read a file into a table type of your choice, or iterate over it directly.

using Pkg
Pkg.add(["CSV", "DataFrames"])

using CSV, DataFrames

# Read into a DataFrame
df = CSV.read("input.csv", DataFrame)

# Or get a lightweight CSV.File and iterate rows
f = CSV.File("input.csv")
for row in f
    println(row.name)
end

# Write any Tables.jl-compatible table
CSV.write("output.csv", df)

CSV.read takes a sink argument, such as DataFrame, that says what kind of table to build. CSV.File returns the package's own column-oriented object, which you can pass to other table types later.

What reading and writing actually change

First, a note on terminology. In this context, a transformation means parsing and serialization. CSV.jl does not reverse, mirror or restyle text. Its transpose option is unrelated to reversing characters. It reads files where each column is stored as a row and each row as a column.

On read: text becomes typed values

  • Type inference: each column is scanned and given a type such as integer, float, date, boolean or string. A field like 00123 can be read as the integer 123, which drops the leading zeros.
  • Missing values: empty fields, and any strings you list in missingstring, become Julia's missing value. The original text of those fields is not kept.
  • Header names: column names come from the header row. With normalizenames enabled, names are adjusted to be valid Julia identifiers, so they may differ from the original header text.
  • Delimiter and quoting: quoted fields are unquoted and doubled quote characters are collapsed. If you do not set delim, the package tries to detect it, so stating it explicitly is safer.
  • Regional number formats: decimal commas are not handled unless you set the decimal option.

Controlling the interpretation

When values must survive unchanged, set types explicitly instead of relying on inference. The example below keeps identifier columns as text and reads a semicolon-delimited file with decimal commas.

df = CSV.read("input.csv", DataFrame;
    delim = ';',
    decimal = ',',
    types = Dict(:zip => String, :account_id => String),
    missingstring = ["", "NA"],
    dateformat = "yyyy-mm-dd")

On write: typed values become text again

  • Fields containing the delimiter, the quote character or a line break are quoted. Embedded quotes are escaped by doubling them, which is the default.
  • Missing values are written as empty fields unless you set missingstring.
  • Numbers and dates are written in Julia's standard text form, not in the original file's formatting. A value read as 1.50 may be written as 1.5, and dates use ISO-style output unless you supply a dateformat.
  • Any values altered during reading, such as stripped leading zeros, stay altered. Writing cannot restore information that parsing discarded.

In short, a read-then-write round trip preserves the meaning of correctly typed data. It does not guarantee identical text. If you need byte-level fidelity, compare the files or keep the original.

Converting between CSV and TSV

Converting a file from one delimiter to another is a read followed by a write with a different delim. To avoid type inference altering values along the way, read every column as a string.

# CSV to TSV, treating every column as text
f = CSV.File("input.csv"; types = String)
CSV.write("output.tsv", f; delim = '\t')

Reading all columns as strings keeps the field content intact. Quoting may still differ from the source, because the writer adds quotes only where its rules require them.

Will the output stay readable and usable after sharing?

CSV is plain text, so the output copies, pastes, diffs and searches like any other text file. In practice, compatibility depends on a few details.

  • Encoding: CSV.jl works with UTF-8 text. Files in other encodings need converting before reading. Some spreadsheet programs expect a byte order mark to recognize UTF-8, and CSV.write offers a bom option for that. Whether a given program needs it should be tested in that program.
  • Regional settings: spreadsheet software in locales that use decimal commas often expects semicolon delimiters. A comma-delimited file may open as a single column there. Choose delim and decimal to match the destination, and check the result there.
  • Pasting into spreadsheets: pasted tab-separated text is commonly split into cells, while comma-separated text often is not. Behavior varies by application, so test the specific destination.
  • Large files: CSV.Rows and CSV.Chunks let you process data without loading everything at once, which matters more than raw speed for very large inputs.

Raw CSV is hard for people to read, and especially so through a screen reader, which will read delimiters and quotes as written. No assistive-technology testing was available for this review, so this is a general caution rather than a measured result. When the audience is human, present the data as a properly marked-up table or a formatted document, and keep the CSV as the machine-readable source.

Gaps in this review

No performance measurements, version-specific changes or tested destination applications were available. Option names and defaults described here reflect the package's documented interface. Check the documentation for the version you install, and test the output in the programs where it will actually be used.