gsv
Types
An error that could occurr trying to parse a csv file.
pub type Error {
UnescapedQuote(line: Int)
MissingClosingQuote(starting_line: Int)
}
Constructors
-
UnescapedQuote(line: Int)
This happens when a field that is not escaped contains a double quote, like this:
first field,second "field",third field
If a field needs to have quotes it should be escaped like this:
first field,"second ""field""",third field
Arguments
- line
-
The line where the unescaped quote is found.
-
MissingClosingQuote(starting_line: Int)
This happens when there’s an escaped field that is missing a closing quote, like this:
first field,"escaped but I'm missing the closing quote some other field,one last field
Arguments
- starting_line
-
The line where the field with the missing closing quote starts.
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
.
Values
pub fn from_dicts(
rows: List(dict.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,
separator field_separator: String,
) -> Result(List(dict.Dict(String, String)), Error)
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(separator: ",")
// 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(
csv: String,
separator separator: String,
) -> Result(List(List(String)), Error)
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(seperator: ",")
// Ok([
// ["hello", " world"],
// ["goodbye", " mars"],
// ])
This implementation tries to stick as closely as possible to RFC4180, with a couple of notable differences:
- both
\n
and\r\n
line endings are accepted.- empty lines are allowed and just ignored.
- lines are not forced to all have the same number of fields.
- a line can start with an empty field
,two,three
.- a line can end with an empty field
one,two,
.