read_csv
result = read_csv(path, [headers=true], [sep=","], [decimal="."])
Input: path, headers, sep, decimal · Output: result
What it is
Reads a CSV file into a Table. path (string) is the file to read. headers (boolean, optional, default true) — false treats the first line as data instead of column names (columns are then auto-named col1, col2, ...). sep (string, optional, default ",") is the field delimiter. decimal (string, optional, default ".") is the decimal-point marker (e.g. sep=";", decimal="," for the European convention).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = read_csv(path, [headers=true], [sep=","], [decimal="."]) | path stringpath (string) is the file to read.headers boolean, optional (default true)headers (boolean, optional, default true) — false treats the first line as data instead of column names (columns are then auto-named col1, col2, ...).sep string, optional (default ",")sep (string, optional, default ",") is the field delimiter.decimal string, optional (default ".")decimal (string, optional, default ".") is the decimal-point marker (e.g. | result Table | Returns a Table with one row per data line and one column per field. table.load(path) is sugar for this (no keyword-argument forwarding — use read_csv directly for headers=/sep=/decimal=). |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
t = read_csv(path, headers = true)
print("rows: {nrow(t)}")
print(head(t, 2))
from agent-guide
write_csv(df, "data.csv")
df2 = read_csv("data.csv")
print(df2)
table.write(df, "data2.csv")
df3 = table.load("data2.csv")
print(df3)
x y - -- 1 10 2 20 3 -5 x y - -- 1 10 2 20 3 -5
from tour
function load_and_process(path)
profiling_mode(true)
data = read_csv(path, headers=true)
result = data.sort_by("value")
profiling_mode(false)
print(profile_stats()) # a real Table: function / calls / seconds
sandbox_mode(true)
try
touch("audit.log") # denied -- sandboxed for this section only
catch err
print("blocked: {err.message}")
end
sandbox_mode(false)
return result
end function
from quickstart
measurements = table(
x = [-1.5, 0.5, 2.0, -0.25, 3.75],
sensor = ["a", "b", "a", "c", "b"]
)
write_csv(measurements, "measurements.csv")
df = read_csv("measurements.csv")
print(nrow(df))
print(describe(df))
write_csv(filter(df, df.x > 0), "positive_only.csv")
print(read_all_text("positive_only.csv"))
5
stat x
----- -------
count 5
mean 0.9
std 2.03562
min -1.5
25% -0.25
50% 0.5
75% 2
max 3.75
x,sensor
0.5,b
2,a
... 1 more linesfrom file-io
How it works
read_csv(path, [headers=], [sep=], [decimal=]) — headers (default true) treats the first line as data instead of column names when false (columns auto-named col1, col2, ...); sep (default ",") is the field delimiter; decimal (default ".") is the decimal-point marker ("," for the European convention). See Table::from_csv_opts's own doc comment for the motivating sep=";" + decimal="," case and why there's no implicit per-field decimal auto-detection.
From the note above this builtin in the interpreter, where the reasoning was written down as it was built.