CSV
Parse and generate CSV (Comma-Separated Values) data. Supports custom delimiters, header rows, and proper escaping of special characters.
import Encoding.CSV
Parsing
csv = "name,age,city\nAlice,30,NYC\nBob,25,LA"
rows = CSV.parse csv
# [["name", "age", "city"], ["Alice", "30", "NYC"], ["Bob", "25", "LA"]]
csv = "name,age\nAlice,30\nBob,25"
records = CSV.parse-headers csv
# [{name: "Alice", age: "30"}, {name: "Bob", age: "25"}]
row = CSV.parse-row "Alice,30,NYC"
# ["Alice", "30", "NYC"]
Custom Delimiters
All parsing functions have -with variants that accept a custom delimiter.
Useful for TSV (tab-separated), pipe-separated, or other formats.
# Parse tab-separated values
tsv = "name\tage\nAlice\t30"
rows = CSV.parse-with "\t" tsv
# Parse pipe-separated values
psv = "name|age\nAlice|30"
rows = CSV.parse-with "|" psv
Formatting
rows = [["name", "age"], ["Alice", "30"]]
csv = CSV.to-csv rows
# "name,age\nAlice,30"
escaped = CSV.escape "Hello, World"
# "\"Hello, World\""
File I/O
File-backed CSV helpers require file authority in scope. Reads require
FileReadAuth, FileAuth, or RootAuth; writes require
FileWriteAuth, FileAuth, or RootAuth. Pure parsing helpers
such as CSV.parse and CSV.generate do not require file authority.
FileReadAuth, FileAuth, or
RootAuth in scope.
import Auth.File.{file-auth, file-read-auth}
main = fn(env: Env) =>
read-auth = file-read-auth (file-auth env.root)
match CSV.read "data.csv"
| Ok rows -> process rows
| Err e -> println "Error reading file: ${e}"
match CSV.auto-read read-auth "data.csv"
| Ok rows -> process rows
| Err e -> println "Error: ${e}"
FileWriteAuth, FileAuth, or
RootAuth in scope.
import Auth.File.{file-auth, file-write-auth}
main = fn(env: Env) =>
rows = [["name", "age"], ["Alice", "30"]]
write-auth = file-write-auth (file-auth env.root)
match CSV.write "output.csv" rows
| Ok _ -> println "Saved successfully"
| Err e -> println "Error: ${e}"