Reading text or csv files and assigning values to variable names

And using CSV and DataFrames:

julia> using DataFrames, CSV

julia> df = CSV.read("MyFile_Horizontal.txt",  DataFrame, header=[:a, :b, :c, :d])
1×4 DataFrame
│ Row │ a     │ b       │ c       │ d     │
│     │ Int64 │ Int64   │ Float64 │ Int64 │
├─────┼───────┼─────────┼─────────┼───────┤
│ 1   │ 5     │ 2450000 │ 5.67e-8 │ 1     │

julia> CSV.write("delim_file.txt", df, header=false)
"delim_file.txt"

shell> cat delim_file.txt
5,2450000,5.67e-8,1

You can also call CSV.read with header=false to let it choose column names.

Edit: I forgot about storing the DataFrame values in individual variables:

julia> a,b,c,d = df[1,:];

julia> CSV.write("delim_file.txt", Tables.table([a b c d]), header=false)
"delim_file.txt"

shell> cat delim_file.txt
5.0,2.45e6,5.67e-8,1.0

but for just a few variables from a single line of text I would rather stay with DelimitedFiles.

5 Likes