Skip to content

Commit 740f608

Browse files
committed
Parsing CSV
1 parent 6267443 commit 740f608

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Examples showing how to do many things in Gleam!
2828

2929
## Formats
3030

31+
- [Parsing CSV](./universal/test/formats/parsing_csv.gleam)
3132
- [Parsing TOML](./universal/test/formats/parsing_toml.gleam)
3233
- [Rendering CSV](./universal/test/formats/rendering_csv.gleam)
3334
- [Rendering HTML](./universal/test/formats/rendering_html.gleam)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//// # Parsing CSV
2+
////
3+
//// The gsv package can be used to parse and render CSV on all targets.
4+
////
5+
//// ## Dependencies
6+
////
7+
//// - https://hex.pm/packages/gsv
8+
9+
import gleam/dict
10+
import gleeunit/should
11+
import gsv
12+
13+
pub fn main_test() {
14+
let csv =
15+
"name,colour,score
16+
Lucy,Pink,100
17+
Nubi,Black,100
18+
"
19+
20+
// The `to_lists` function can be used to parse a CSV to a list of lists
21+
gsv.to_lists(csv)
22+
|> should.be_ok
23+
|> should.equal([
24+
["name", "colour", "score"],
25+
["Lucy", "Pink", "100"],
26+
["Nubi", "Black", "100"],
27+
])
28+
29+
// It may be easier to work with your data as dicts. If so you can use the
30+
// `to_dicts` function.
31+
gsv.to_dicts(csv)
32+
|> should.be_ok
33+
|> should.equal([
34+
dict.from_list([#("name", "Lucy"), #("colour", "Pink"), #("score", "100")]),
35+
dict.from_list([#("name", "Nubi"), #("colour", "Black"), #("score", "100")]),
36+
])
37+
}

0 commit comments

Comments
 (0)