← All ivy-nodes
IVYXSTUDIO · IVY NODE
C
CSV Read
ivy.node.csv-read · v0.1.0
ivyx✓
Reads a CSV file, or CSV text, into rows with the column names and row count. Numbers come back as numbers and empty cells as null.
#csv#data#table#read
Inputs
| Field | Type | Description |
|---|---|---|
| path | string | Path to the CSV file. |
| text | string | CSV text, instead of a path. |
| delimiter | string | The field separator. |
Outputs
| Field | Type | Description |
|---|---|---|
| rowsrequired | array | One object per row, keyed by column. |
| columnsrequired | array | Column names in file order. |
| row_countrequired | integer | How many rows were read. |
Source
python
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
import json as _json
def frame_rows(frame):
"""A DataFrame as JSON rows: NaN becomes null, dates ISO, numpy plain."""
return _json.loads(frame.to_json(orient="records", date_format="iso"))
import io
import pandas as pd
path = inp.get("path")
text = inp.get("text")
delimiter = inp.get("delimiter", ",")
if bool(path) == bool(text):
raise ValueError("Give exactly one of path or text.")
frame = pd.read_csv(path if path else io.StringIO(text), sep=delimiter)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out["rows"] = frame_rows(frame)
out["columns"] = [str(c) for c in frame.columns]
out["row_count"] = int(len(frame))Tests
Requires: python:3.9, pandas
- from-file
A file with a missing cell reads into rows, the gap as null.
- from-text
Semicolon-separated text reads with its delimiter.
- neither
With no path and no text there is nothing to read.