Table of Contents

csv

csv parses and writes comma-separated values files

Functions

read_all

read_all(source, comma=",", comment="", lazy_quotes=False, trim_leading_space=False, fields_per_record=0, skip=0) [][]string

read all rows from a source string, returning a list of string lists

parameters:

nametypedescription
sourcestringinput string of csv data
commastringcomma is the field delimiter, defaults to "," (a comma). comma must be a valid character and must not be \r, \n, or the Unicode replacement character (0xFFFD).
commentstringcomment, if not "", is the comment character. Lines beginning with the comment character without preceding whitespace are ignored. With leading whitespace the comment character becomes part of the field, even if trim_leading_space is True. comment must be a valid character and must not be \r, \n, or the Unicode replacement character (0xFFFD). It must also not be equal to comma.
lazy_quotesboolIf lazy_quotes is True, a quote may appear in an unquoted field and a non-doubled quote may appear in a quoted field.
trim_leading_spaceboolIf trim_leading_space is True, leading white space in a field is ignored. This is done even if the field delimiter, comma, is white space.
fields_per_recordintfields_per_record is the number of expected fields per record. If fields_per_record is positive, read_all requires each record to have the given number of fields. If fields_per_record is 0, read_all sets it to the number of fields in the first record, so that future records must have the same field count. If fields_per_record is negative, no check is made and records may have a variable number of fields.
skipintnumber of rows to skip, omitting from returned rows

examples:

basic

read a csv string into a list of string lists

load("encoding/csv.star", "csv")
data_str = """type,name,number_of_legs
dog,spot,4
cat,spot,3
spider,samantha,8
"""
data = csv.read_all(data_str)
print(data)
# Output: [["type", "name", "number_of_legs"], ["dog", "spot", "4"], ["cat", "spot", "3"], ["spider", "samantha", "8"]]

write_all

write_all(source,comma=",") string

write all rows from source to a csv-encoded string

parameters:

nametypedescription
source[][]stringarray of arrays of strings to write to csv
commastringcomma is the field delimiter, defaults to "," (a comma). comma must be a valid character and must not be \r, \n, or the Unicode replacement character (0xFFFD).

examples:

basic

write a list of string lists to a csv string

load("encoding/csv.star", "csv")
data = [
["type", "name", "number_of_legs"],
["dog", "spot", "4"],
["cat", "spot", "3"],
["spider", "samantha", "8"],
]
csv_str = csv.write_all(data)