Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

polars `read_csv()` to read from string and not from file

is it possible to read from string with pl.read_csv() ? Something like this, which would work :

content = """c1, c2
            A,1,
            B,3
            C,2"""
pl.read_csv(content)

I know of course about this :

pl.DataFrame({"c1":["A", "B", "C"],"c2" :[1,3,2]})

But it is error-prone with long tables and you have to count numbers to know which value to modify.
I also know about dictionnaries but I have more than 2 columns in my real life example.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Context : I used to fread() content with R data.table and it was very useful, especially when you want to convert a column with the help of a join, instead of complicated ifelse() statements

Thanks !

>Solution :

pl.read_csv() accepts IO as source parameter.

source: str | Path | IO[str] | IO[bytes] | bytes

So you can use io.StringIO:

from io import StringIO

content = """
c1,c2
A,1
B,3
C,2
"""

data = StringIO(content)
pl.read_csv(data)
shape: (3, 2)
┌─────┬─────┐
│ c1  ┆ c2  │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════╪═════╡
│ A   ┆ 1   │
│ B   ┆ 3   │
│ C   ┆ 2   │
└─────┴─────┘

As you can see above, you can also pass bytes as source parameter. You can use str.encode() method for that:

content = """
c1,c2
A,1
B,3
C,2
"""

pl.read_csv(content.encode())
shape: (3, 2)
┌─────┬─────┐
│ c1  ┆ c2  │
│ --- ┆ --- │
│ str ┆ i64 │
╞═════╪═════╡
│ A   ┆ 1   │
│ B   ┆ 3   │
│ C   ┆ 2   │
└─────┴─────┘
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading