Comprehensive Guide to File I/O in Julia
Overview
This document summarizes packages and functions related to file I/O in Julia.
Summary
- CSV: CSV.jl
- Reading
CSV.read("file_name.csv", DataFrame)
CSV.File("file_name.csv")
- Writing:
CSV.write("file_name.csv", df_data)
- Reading
- JSON: JSON3.jl, JSON.jl
- Reading
- String:
read("file_name.json", String)
- JSON3.Object:
JSON3.read(cd*"/wonnyo.json")
- Dictionary:
JSON3.read(cd*"/wonnyo.json") |> Dict
- String:
- Writing
JSON3.write("file_name.json", json_data)
open("file_name.json", "w") do io; JSON3.pretty(io, json_data); end
- Reading
- Pickle: PyCall.jl
- Reading:
open("file_name.pkl") do f; pickle.load(f); end
- Writing:
open("file_name.pkl", "w") do f; pickle.dump(dict_data, f); end
- Reading:
- Numpy: PyCall.jl
- Reading:
open(joinpath(cd, "file_name.npy")) do f; np.load(f); end
- Writing:
open(joinpath(cd, "file_name.npy"), "w") do f; np.save(f, vector_data); end
- Reading:
- MATLAB: MAT.jl
- Reading:
matread("file_name.mat")
- Writing:
matwrite("file_name.mat", mat_data)
- Reading:
- XLSX File: XLSX.jl
- Reading:
XLSX.readtable("file_name.xlsx", "Sheet1") |> DataFrame
- Writing:
XLSX.writetable("file_name.xlsx", df_data)
- Reading:
CSV
- Required Packages:
CSV.jl
,DataFrames.jl
- Official Documentation
- Posts:
using CSV, DataFrames
df = CSV.read("file_name.csv", DataFrame) # CSV 파일을 데이터프레임으로 읽기
CSV.write(joinpath(cd, "file_name.csv"), df) # 데이터프레임을 CSV 파일로 쓰기
# CSV 파일 그 자체로 읽기
csv_file = CSV.File("file_name.csv")
Environment
- Date: 2025/06/12
- OS: Windows11
- Version: Julia 1.11.3, CSV v0.10.15, DataFrames v1.7.0
JSON
- Required Packages:
JSON.jl
,JSON3.jl
- Official Documentation
- Posts:
using JSON3
str_from_json = read("file_name.json", String) # 문자열로 불러오기
json_object = JSON3.read(cd*"/wonnyo.json") # JSON3.Object 타입으로 불러오기
dcit_from_json = JSON3.read(cd*"/wonnyo.json") |> Dict # 딕셔너리로 불러오기
# 문자열을 JSON으로 쓰기
json_data = JSON3.read("""{"property1": 1, "property2": 2, "property3": "test"}""")
JSON3.write("file_name.json", json_data)
# 예쁜 형식으로 쓰기
json_data = JSON3.read("""{"property1": 1, "property2": 2, "property3": "test"}""")
open("file_name.json", "w") do io
JSON3.pretty(io, json_data)
end
Environment
- Date: 2025/06/12
- OS: Windows11
- Version: Julia 1.11.3, JSON3 v1.14.2
Pickle
- Required Packages:
PyCall.jl
using PyCall
pickle = pyimport("pickle")
dict_data = Dict("key1" => "value1", "key2" => "value2")
# 딕셔너리를 pkl 파일로 쓰기
open("file_name.pkl", "w") do f
pickle.dump(dict_data, f)
end
# pkl 파일을 딕셔너리로 읽기
dict_from_pkl = open("file_name.pkl") do f
pickle.load(f)
end
Environment
- Date: 2025/06/12
- OS: Windows11
- Version: Julia 1.11.3, PyCall v1.96.4
Numpy
- Required Packages:
PyCall.jl
- Posts:
using PyCall
np = pyimport("numpy")
vector_data = [1.0, 2, 3]
# 벡터를 npy 파일로 쓰기
open(joinpath(cd, "file_name.npy"), "w") do f
np.save(f, vector_data)
end
# npy 파일을 벡터로 읽기
vec_from_npy = open(joinpath(cd, "file_name.npy")) do f
np.load(f)
end
Environment
- Date: 2025/06/12
- OS: Windows11
- Version: Julia 1.11.3, PyCall v1.96.4
MATLAB
- Required Packages:
MAT.jl
- Official Documentation
- Posts:
using MAT
mat_data = Dict("matrix1" => [1.0 2.0; 3.0 4.0], "matrix2" => [5.0 6.0; 7.0 8.0])
matwrite("file_name.mat", mat_data) # 딕셔너리를 mat 파일로 쓰기
mat_data_from_file = matread("file_name.mat") # mat 파일을 딕셔너리로 읽기
Environment
- Date: 2025/06/12
- OS: Windows11
- Version: Julia 1.11.3, MAT v0.10.7
XLSX Files
- Required Packages:
XLSX.jl
- Official Documentation
- Posts:
using XLSX
using DataFrames
df_data = DataFrame(Arabic = 1:5,
English = ["one", "two", "three", "four", "five"],
Korean = ["일", "이", "삼", "사", "오"])
XLSX.writetable("file_name.xlsx", df_data)
df_from_xlsx = XLSX.readtable("file_name.xlsx", "Sheet1") |> DataFrame
Environment
- Date: 2025/06/12
- OS: Windows11
- Version: Julia 1.11.3, XLSX v0.10.4