gsv
Types
Possible line endings used when turning a parsed csv back into a string
with the from_lists
and from_dicts
functions.
pub type LineEnding {
Windows
Unix
}
Constructors
-
Windows
The CRLF line ending:
\r\n
. -
Unix
The LF line ending:
\n
.
pub type ParseError {
UnescapedQuote(position: Int)
UnclosedEscapedField(start: Int)
}
Constructors
-
UnescapedQuote(position: Int)
This error can occur if there is a csv field contains an unescaped double quote
"
.A field can contain a double quote only if it is escaped (that is, surrounded by double quotes). For example
wibb"le
would be an invalid field, the correct way to write such a field would be like this:"wibb""le"
.Arguments
-
position
The byte index of the unescaped double.
-
-
UnclosedEscapedField(start: Int)
This error can occur when the file ends without the closing
"
of an escaped field. For example:"hello
.Arguments
-
start
The byte index of the start of the unclosed escaped field.
-
Functions
pub fn from_dicts(
rows: List(Dict(String, String)),
separator separator: String,
line_ending line_ending: LineEnding,
) -> String
Takes a list of dicts and writes it to a csv string.
Will automatically escape strings that contain double quotes or
line endings with double quotes (in csv, double quotes get escaped by doing
a double doublequote)
The string he"llo\n
becomes "he""llo\n"
pub fn from_lists(
rows: List(List(String)),
separator separator: String,
line_ending line_ending: LineEnding,
) -> String
Takes a list of lists of strings and turns it to a csv string, automatically escaping all fields that contain double quotes or line endings.
Examples
let rows = [["hello", "world"], ["goodbye", "mars"]]
from_lists(rows, separator: ",", line_ending: Unix)
// "hello,world
// goodbye,mars"
let rows = [[]]
pub fn to_dicts(
input: String,
) -> Result(List(Dict(String, String)), ParseError)
Parses a csv string into a list of dicts: the first line of the csv is interpreted as the headers’ row and each of the following lines is turned into a dict with a value for each of the headers.
If a field is empty then it won’t be added to the dict.
Examples
"pet,name,cuteness
dog,Fido,100
cat,,1000
"
|> gsv.to_dicts
// Ok([
// dict.from_list([
// #("pet", "dog"), #("name", "Fido"), #("cuteness", "100")
// ]),
// dict.from_list([
// #("pet", "cat"), #("cuteness", "1000")
// ]),
// ])
Just list
to_lists
this implementation tries to stick as closely as possible to RFC4180. You can look atto_lists
’ documentation to see how it differs from the RFC.
pub fn to_lists(
input: String,
) -> Result(List(List(String)), ParseError)
Parses a csv string into a list of lists of strings: each line of the csv will be turned into a list with an item for each field.
Examples
"hello, world
goodbye, mars"
|> gsv.to_lists
// Ok([
// ["hello", " world"],
// ["goodbye", " mars"],
// ])
This implementation tries to stick as closely as possible to RFC4180, with a couple notable convenience differences:
- both
\n
and\r\n
line endings are accepted.- a line can start with an empty field
,two,three
.- empty lines are allowed and just ignored.
- lines are not forced to all have the same number of fields.
- a line can end with a comma (meaning its last field is empty).