Introduction to the Julia programming language

10 Filesystem¶

The do syntax we saw earlier is helpful when using the open() function:

In [1]:
open("test.txt", "w") do f
    write(f, "This is a test.\n")
    write(f, "I repeat, this is a test.\n")
end

open("test.txt") do f
    for line in eachline(f)
        println("[$line]")
    end
end
[This is a test.]
[I repeat, this is a test.]

Alternatively, you can read the whole file into a string:

In [2]:
s = read("test.txt", String)
"This is a test.\nI repeat, this is a test.\n"

Reading a file from a URL¶

In [3]:
using CSV, HTTP, DataFrames
ENV["DATAFRAMES_ROWS"] = 6;

url = "https://www.physi.uni-heidelberg.de/~reygers/lectures/2021/ml/data/magic04_data.txt"

# Read the CSV file from the URL
response = HTTP.get(url)
df = CSV.read(IOBuffer(response.body), DataFrame)
19020×11 DataFrame
19014 rows omitted
RowfLengthfWidthfSizefConcfConc1fAsymfM3LongfM3TransfAlphafDistclass
Float64Float64Float64Float64Float64Float64Float64Float64Float64Float64String1
128.796716.00212.64490.39180.198227.700422.011-8.202740.09281.8828g
231.603611.72352.51850.53030.377326.272223.8238-9.95746.3609205.261g
3162.052136.0314.06120.03740.0187116.741-64.858-45.21676.96256.788g
⋮⋮⋮⋮⋮⋮⋮⋮⋮⋮⋮⋮
1901875.445547.53053.44830.14170.0549-9.356141.0562-9.466230.2987256.517h
19019120.51376.90183.99390.09440.06835.8043-93.5224-63.838984.6874408.317h
19020187.18153.00143.20930.28760.1539-167.312-168.45631.475552.731272.317h
In [ ]: