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
Row | fLength | fWidth | fSize | fConc | fConc1 | fAsym | fM3Long | fM3Trans | fAlpha | fDist | class |
---|---|---|---|---|---|---|---|---|---|---|---|
Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | Float64 | String1 | |
1 | 28.7967 | 16.0021 | 2.6449 | 0.3918 | 0.1982 | 27.7004 | 22.011 | -8.2027 | 40.092 | 81.8828 | g |
2 | 31.6036 | 11.7235 | 2.5185 | 0.5303 | 0.3773 | 26.2722 | 23.8238 | -9.9574 | 6.3609 | 205.261 | g |
3 | 162.052 | 136.031 | 4.0612 | 0.0374 | 0.0187 | 116.741 | -64.858 | -45.216 | 76.96 | 256.788 | g |
⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ | ⋮ |
19018 | 75.4455 | 47.5305 | 3.4483 | 0.1417 | 0.0549 | -9.3561 | 41.0562 | -9.4662 | 30.2987 | 256.517 | h |
19019 | 120.513 | 76.9018 | 3.9939 | 0.0944 | 0.0683 | 5.8043 | -93.5224 | -63.8389 | 84.6874 | 408.317 | h |
19020 | 187.181 | 53.0014 | 3.2093 | 0.2876 | 0.1539 | -167.312 | -168.456 | 31.4755 | 52.731 | 272.317 | h |
In [ ]: