Skip to content

Commit fb96984

Browse files
committed
mojo_csv
1 parent 928666d commit fb96984

File tree

4 files changed

+142
-0
lines changed

4 files changed

+142
-0
lines changed

recipes/mojo_csv/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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)):
19+
print(reader[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)):
32+
print(reader[i])
33+
```
34+
35+
### Attributes
36+
37+
```mojo
38+
reader.raw : String # raw csv string
39+
reader.raw_length : Int # total number of Chars
40+
reader.headers : List[String] # first row of csv file
41+
reader.row_count : Int # total number of rows T->B
42+
reader.column_count : Int # total number of columns L->R
43+
reader.elements : List[String] # all delimited elements
44+
reader.length : Int # total number of elements
45+
```
46+
47+
##### Indexing
48+
49+
currently the array is only 1D, so indexing is fairly manual.
50+
51+
```Mojo
52+
reader[0] # first element
53+
```

recipes/mojo_csv/recipe.yaml

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
context:
2+
version: 1.2.0
3+
4+
package:
5+
name: "mojo_csv"
6+
version: ${{ version }}
7+
8+
source:
9+
- git: https://github.com/Phelsong/mojo_csv
10+
rev: e642c060a6fad04b1a42727501c386f812b88e8d
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

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
item1,item2,"ite,em3"
2+
"p""ic", pi c,pic,
3+
r_i_1,"r_i_2""",r_i_3,

recipes/mojo_csv/test_pack.mojo

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
'"p""ic"',
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("parsing:", in_csv)
25+
print("----------")
26+
try:
27+
assert_true(rd.col_count == 3)
28+
for x in range(len(rd)):
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+
print("----------")
37+
print("columns:", rd.col_count, "of 3")
38+
print("rows:", rd.row_count, "of 3")
39+
assert_true(rd.row_count == 3)
40+
print("elements:", rd.__len__(), "of 9")
41+
assert_true(len(rd.elements) == 9)
42+
except AssertionError:
43+
print(AssertionError)
44+
raise AssertionError
45+
print("----------")
46+
print("parse successful")

0 commit comments

Comments
 (0)