logo

How to Read CSV as a String Directly into a DataFrame in Julia 📂Julia

How to Read CSV as a String Directly into a DataFrame in Julia

Code

If you don’t have it as a file but have simple CSV data as a string, or need fine-grained manipulation at the level of directly editing the string, you can pass it directly to CSV.read using an IOBuffer.

using CSV
using DataFrames

csv_string = """
Name,Age,City
Alice,30,Seoul
Bob,25,Busan
Charlie,35,Daegu
"""

io = IOBuffer(csv_string)
df = CSV.read(io, DataFrame)
julia> df = CSV.read(io, DataFrame)
3×3 DataFrame
 Row │ Name     Age    City    
     │ String7  Int64  String7
─────┼─────────────────────────
   1 │ Alice       30  Seoul
   2 │ Bob         25  Busan
   3 │ Charlie     35  Daegu

Environment

  • OS: Windows
  • julia: v1.12.0