Julia: How to Read A CSV File from Online

Hongtao Hao / 2021-06-21


The following codes were tested under Julia v1.6.1.

Take this csv for example: https://covid.ourworldindata.org/data/owid-covid-data.csv

We’ll need to use three packages: DataFrames, CSV, and HTTP. If you don’t have them installed already, use:

import Pkg; Pkg.add(["DataFrames", "CSV", "HTTP"])
# Based on https://github.com/JuliaLang/julia/issues/19591#issuecomment-303877707

If you simply run:

using DataFrames, CSV
df = CSV.read("https://covid.ourworldindata.org/data/owid-covid-data.csv", DataFrame)

You’ll see an error:

ArgumentError: "https://covid.ourworldindata.org/data/owid-covid-data.csv" is not a valid file

Thankfully, I found the solution in bkamins’s DataFrames.jl tutorial :

using DataFrames, CSV, HTTP
input = "https://covid.ourworldindata.org/data/owid-covid-data.csv"
df = CSV.read(HTTP.get(input).body, DataFrame)
# Alternatively, you can use `df = DataFrame(CSV.File(HTTP.get(input).body))`

Last modified on 2021-10-05