Mitchell
Mitchell

Reputation: 75

Is there a way to read from stdin with polars?

Is there a way to read from stdin with polars? I have tried a few different ways and always hit errors.

$ printf "1,2,3\n1,2,3\n" | python -c 'import polars as pl; import sys; pl.read_csv("/dev/stdin")'

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/mmfs1/gscratch/stergachislab/mvollger/miniconda3/envs/snakemake/lib/python3.9/site-packages/polars/io.py", line 398, in read_csv
    df = DataFrame._read_csv(
  File "/mmfs1/gscratch/stergachislab/mvollger/miniconda3/envs/snakemake/lib/python3.9/site-packages/polars/internals/frame.py", line 585, in _read_csv
    self._df = PyDataFrame.read_csv(
OSError: No such device (os error 19)

Thanks in advance, Mitchell

Upvotes: 1

Views: 557

Answers (1)

mutecamel
mutecamel

Reputation: 366

$ printf "a,b,c\n1,2,3\n" | python3 -c 'import polars as pl; import sys; import io; print(pl.read_csv(io.StringIO(sys.stdin.read())))'
shape: (1, 3)
┌─────┬─────┬─────┐
│ a   ┆ b   ┆ c   │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1   ┆ 2   ┆ 3   │
└─────┴─────┴─────┘

Just read from STDIN, then wrap the str into StringIO, and pass it to polars.read_csv.

Upvotes: 3

Related Questions