Skip to content

Commit fec68dc

Browse files
committed
mojo_csv
1 parent 928666d commit fec68dc

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

recipes/mojo_csv/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Mojo Csv
2+
3+
Csv parsing library written in pure Mojo
4+
5+
### usage
6+
7+
Add the Modular community channel (https://repo.prefix.dev/modular-community) to your mojoproject.toml file or pixi.toml file in the channels section.
8+
9+
##### Basic Usage
10+
11+
```mojo
12+
from mojo_csv import CsvReader
13+
from pathlib import Path
14+
15+
fn main():
16+
var csv_path = Path("path/to/csv/file.csv")
17+
var reader = CsvReader(csv_path)
18+
for i in range(len(reader.elements)):
19+
print(reader.elements[i])
20+
```
21+
22+
##### Optional Usage
23+
24+
```mojo
25+
from mojo_csv import CsvReader
26+
from pathlib import Path
27+
28+
fn main():
29+
var csv_path = Path("path/to/csv/file.csv")
30+
var reader = CsvReader(csv_path, delimiter="|", quotation_mark='*')
31+
for i in range(len(reader.elements)):
32+
print(reader.elements[i])
33+
```
34+
35+
### Attributes
36+
37+
```mojo
38+
reader.raw
39+
reader.headers
40+
reader.row_count
41+
reader.column_count
42+
reader.length
43+
```

recipes/mojo_csv/recipe.yaml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
context:
2+
version: 1.1.0
3+
4+
package:
5+
name: "mojo_csv"
6+
version: ${{ version }}
7+
8+
source:
9+
- git: https://github.com/Phelsong/mojo_csv
10+
rev: 378626bf96e8b9d2a849b0d39b77c10732db11c4
11+
12+
build:
13+
number: 0
14+
script:
15+
- mojo package src -o ${{ PREFIX }}/lib/mojo/mojo_csv.mojopkg
16+
17+
requirements:
18+
host:
19+
- max >=25.1.0,<26
20+
run:
21+
- ${{ pin_compatible('max') }}
22+
23+
tests:
24+
- script:
25+
- if: unix
26+
then:
27+
- "mojo test_pack.mojo test.csv"
28+
29+
about:
30+
homepage: https://github.com/Phelsong/mojo_csv
31+
license: APACHE-2.0
32+
license_file: LICENSE
33+
summary: Csv parsing library written in pure Mojo
34+
repository: https://github.com/Phelsong/mojo_csv
35+
36+
extra:
37+
maintainers:
38+
- phelsong
39+
project_name:
40+
- mojo_csv

recipes/mojo_csv/test.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
item1,item2,"ite,em3"
2+
pic, pi c,pic,
3+
r_i_1,r_i_2,r_i_3,

recipes/mojo_csv/test_pack.mojo

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from collections import Dict, List
2+
from pathlib import Path, cwd
3+
from sys import argv, exit
4+
from testing import assert_true
5+
6+
from mojo_csv import CsvReader
7+
8+
var VALID = List[String](
9+
"item1",
10+
"item2",
11+
'"ite,em3"',
12+
"pic",
13+
" pi c",
14+
"pic",
15+
"r_i_1",
16+
"r_i_2",
17+
"r_i_3",
18+
)
19+
20+
21+
fn main() raises:
22+
var in_csv: Path = Path(argv()[1])
23+
var rd = CsvReader(in_csv)
24+
print(in_csv)
25+
print("columns:", rd.col_count)
26+
print("----------")
27+
try:
28+
for x in range(len(rd.elements)):
29+
print(rd.elements[x])
30+
assert_true(
31+
rd.elements[x] == VALID[x],
32+
String("[{0}] != expected [{1}] at index {2}").format(
33+
rd.elements[x], VALID[x], x
34+
),
35+
)
36+
assert_true(len(rd.elements) == 9)
37+
except AssertionError:
38+
raise AssertionError
39+
print("----------")
40+
print("parse successful")
41+

0 commit comments

Comments
 (0)