Standard library · Encoding and data

encoding/csv

Reading and writing CSV files, with correct quoting and conversion to a dynamic value.

import encoding.csv
On this page
  1. Types
  2. Functions

CSV is the simplest table format: one row per line, fields separated by commas. A field containing a comma, a quote or a newline is wrapped in quotes, and a quote inside it is doubled. This package follows those rules both ways.

Every field is a string. When the first row is a header, ToValue turns each row into an object with those keys.

Types

CSVRow

struct CSVRow
One row.
NameTypeDescription
fieldsVectorThe row's fields.
encoding-csv-Table.salam
import encoding.csv
import encoding.value

func main:
    text := "name,city\nAli,Tehran\n\"Sara, engineer\",\"Shiraz\"\n"
    mut table := csv.ReadAll(text)
    println table.rows.len()
    each row in table.rows:
        println row.fields.len(), row.fields[0]
    end
    println csv.WriteAll(table)
    csv.FreeTable(table)
    println csv.ReadLine(`a,"b,c",d`)
    mut fields := Vector {} as Vector<str>
    fields.push("plain")
    fields.push(`with "quotes"`)
    println csv.WriteLine(fields)
    v := csv.ToValue("name,age\nAli,30\n")
    println value.ArrayLen(v), value.AsString(value.ObjectGet(value.ArrayGet(v, 0), "name"))
    println csv.FromValue(v)
    opts := csv.CSVOptions { header = false }
    rows := csv.ToValueWith("1,2\n3,4\n", opts)
    println value.ArrayLen(rows), csv.FromValueWith(rows, opts)
end
Output3 2 name 2 Ali 2 Sara, engineer name,city Ali,Tehran "Sara, engineer",Shiraz ["a", "b,c", "d"] plain,"with ""quotes""" 1 Ali age,name 30,Ali 2 1,2 3,4

CSVTable

struct CSVTable
A table.
NameTypeDescription
rowsVectorThe rows, including the header row.

The example for this item is shown with the ones above: encoding-csv-Table.salam

CSVOptions

struct CSVOptions
The options for converting to and from a value.
NameTypeDescription
headerbool = trueThe first row is a header; true by default. When false, each row becomes an array.

The example for this item is shown with the ones above: encoding-csv-Table.salam

Functions

ReadLine

func ReadLine(line: str): Vector<str>
Splits one CSV line into its fields.

The example for this item is shown with the ones above: encoding-csv-Table.salam

WriteLine

func WriteLine(fields: Vector<str>): str
Turns fields into one CSV line, quoting wherever it has to.

The example for this item is shown with the ones above: encoding-csv-Table.salam

ReadAll

func ReadAll(text: str): CSVTable
Turns a whole piece of CSV text into a table.

The example for this item is shown with the ones above: encoding-csv-Table.salam

FreeTable

func FreeTable(t &: CSVTable)
Releases the table's memory.

The example for this item is shown with the ones above: encoding-csv-Table.salam

WriteAll

func WriteAll(table: CSVTable): str
Turns a table into CSV text.

The example for this item is shown with the ones above: encoding-csv-Table.salam

ToValueWith

func ToValueWith(text: str, opts: CSVOptions): value.Value
Turns CSV into a Value with the given options.

The example for this item is shown with the ones above: encoding-csv-Table.salam

ToValue

func ToValue(text: str): value.Value
Turns CSV with a header into an array of objects.

The example for this item is shown with the ones above: encoding-csv-Table.salam

FromValueWith

func FromValueWith(v: value.Value, opts: CSVOptions): str
Turns an array of objects, or of arrays, into CSV with the given options.

The example for this item is shown with the ones above: encoding-csv-Table.salam

FromValue

func FromValue(v: value.Value): str
Turns an array of objects into CSV with a header.

The example for this item is shown with the ones above: encoding-csv-Table.salam