Skip to content

mojo_csv #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions recipes/mojo_csv/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Mojo Csv

Csv parsing library written in pure Mojo

### usage

Add the Modular community channel (https://repo.prefix.dev/modular-community) to your mojoproject.toml file or pixi.toml file in the channels section.

##### Basic Usage

```mojo
from mojo_csv import CsvReader
from pathlib import Path

fn main():
var csv_path = Path("path/to/csv/file.csv")
var reader = CsvReader(csv_path)
for i in range(len(reader)):
print(reader[i])
```

##### Optional Usage

```mojo
from mojo_csv import CsvReader
from pathlib import Path

fn main():
var csv_path = Path("path/to/csv/file.csv")
var reader = CsvReader(csv_path, delimiter="|", quotation_mark='*')
for i in range(len(reader)):
print(reader[i])
```

### Attributes

```mojo
reader.raw : String # raw csv string
reader.raw_length : Int # total number of Chars
reader.headers : List[String] # first row of csv file
reader.row_count : Int # total number of rows T->B
reader.column_count : Int # total number of columns L->R
reader.elements : List[String] # all delimited elements
reader.length : Int # total number of elements
```

##### Indexing

currently the array is only 1D, so indexing is fairly manual.

```Mojo
reader[0] # first element
```
43 changes: 43 additions & 0 deletions recipes/mojo_csv/recipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
context:
version: 1.2.0

package:
name: "mojo_csv"
version: ${{ version }}

source:
- git: https://github.com/Phelsong/mojo_csv.git
rev: e642c060a6fad04b1a42727501c386f812b88e8d

build:
number: 0
script:
- mojo package src -o ${{ PREFIX }}/lib/mojo/mojo_csv.mojopkg

requirements:
host:
- max >=25.1.0,<26
run:
- ${{ pin_compatible('max') }}

tests:
- script:
- if: unix
then:
- mojo run test.mojo
files:
recipe:
- test.csv

about:
homepage: https://github.com/Phelsong/mojo_csv
license: Apache-2.0
license_file: LICENSE
summary: Csv parsing library written in pure Mojo
repository: https://github.com/Phelsong/mojo_csv

extra:
maintainers:
- phelsong
project_name:
- mojo_csv
3 changes: 3 additions & 0 deletions recipes/mojo_csv/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
item1,item2,"ite,em3"
"p""ic", pi c,pic,
r_i_1,"r_i_2""",r_i_3,
45 changes: 45 additions & 0 deletions recipes/mojo_csv/test.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from pathlib import Path, cwd
from sys import argv, exit
from testing import assert_true

from src.csv_reader import CsvReader

var VALID = List[String](
"item1",
"item2",
'"ite,em3"',
'"p""ic"',
" pi c",
"pic",
"r_i_1",
'"r_i_2"""',
"r_i_3",
)


fn main() raises:
var in_csv: Path = Path(argv()[1])
var rd = CsvReader(in_csv)
print("parsing:", in_csv)
print("----------")
try:
assert_true(rd.col_count == 3)
for x in range(len(rd)):
print(rd.elements[x])
assert_true(
rd.elements[x] == VALID[x],
String("[{0}] != expected [{1}] at index {2}").format(
rd.elements[x], VALID[x], x
),
)
print("----------")
print("columns:", rd.col_count, "of 3")
print("rows:", rd.row_count, "of 3")
assert_true(rd.row_count == 3)
print("elements:", rd.__len__(), "of 9")
assert_true(len(rd.elements) == 9)
except AssertionError:
print(AssertionError)
raise AssertionError
print("----------")
print("parse successful")
46 changes: 46 additions & 0 deletions recipes/mojo_csv/test_pack.mojo
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from collections import Dict, List
from pathlib import Path, cwd
from sys import argv, exit
from testing import assert_true

from mojo_csv import CsvReader

var VALID = List[String](
"item1",
"item2",
'"ite,em3"',
'"p""ic"',
" pi c",
"pic",
"r_i_1",
'"r_i_2"""',
"r_i_3",
)


fn main() raises:
var in_csv: Path = Path(argv()[1])
var rd = CsvReader(in_csv)
print("parsing:", in_csv)
print("----------")
try:
assert_true(rd.col_count == 3)
for x in range(len(rd)):
print(rd.elements[x])
assert_true(
rd.elements[x] == VALID[x],
String("[{0}] != expected [{1}] at index {2}").format(
rd.elements[x], VALID[x], x
),
)
print("----------")
print("columns:", rd.col_count, "of 3")
print("rows:", rd.row_count, "of 3")
assert_true(rd.row_count == 3)
print("elements:", rd.__len__(), "of 9")
assert_true(len(rd.elements) == 9)
except AssertionError:
print(AssertionError)
raise AssertionError
print("----------")
print("parse successful")
Loading