Skip to content

Commit

Permalink
Parsing CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Dec 7, 2024
1 parent 6267443 commit 740f608
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Examples showing how to do many things in Gleam!

## Formats

- [Parsing CSV](./universal/test/formats/parsing_csv.gleam)
- [Parsing TOML](./universal/test/formats/parsing_toml.gleam)
- [Rendering CSV](./universal/test/formats/rendering_csv.gleam)
- [Rendering HTML](./universal/test/formats/rendering_html.gleam)
Expand Down
37 changes: 37 additions & 0 deletions universal/test/formats/parsing_csv.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//// # Parsing CSV
////
//// The gsv package can be used to parse and render CSV on all targets.
////
//// ## Dependencies
////
//// - https://hex.pm/packages/gsv

import gleam/dict
import gleeunit/should
import gsv

pub fn main_test() {
let csv =
"name,colour,score
Lucy,Pink,100
Nubi,Black,100
"

// The `to_lists` function can be used to parse a CSV to a list of lists
gsv.to_lists(csv)
|> should.be_ok
|> should.equal([
["name", "colour", "score"],
["Lucy", "Pink", "100"],
["Nubi", "Black", "100"],
])

// It may be easier to work with your data as dicts. If so you can use the
// `to_dicts` function.
gsv.to_dicts(csv)
|> should.be_ok
|> should.equal([
dict.from_list([#("name", "Lucy"), #("colour", "Pink"), #("score", "100")]),
dict.from_list([#("name", "Nubi"), #("colour", "Black"), #("score", "100")]),
])
}

0 comments on commit 740f608

Please sign in to comment.