Reading text or csv files and assigning values to variable names

For writedlm to write the values on the same line, you need to do something like this:

julia> using DelimitedFiles

julia> a,b,c,d = readdlm("MyFile_Horizontal.txt", ',');

julia> writedlm("delim_file.txt", [a b c d], ", ")

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

Explanation: writedlm respects the rows/columns of a matrix. For other types of collections, it iterates and writes one value per line. From the documentation:

Write A (a vector, matrix, or an iterable collection of iterable rows) as text to f

So it iterates on the tuple created with x = a,b,c,d, and you get values on different lines.

3 Likes

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

I like this solution. I have added a couple of extra lines to that reflect the intermediate operations on a, b,c … and then print out the new result. Many thanks for your clear explanation. It seems a lot simpler than using CSV. Regards Peter


 a,b,c,d = readdlm("C:\\Users\\peter\\Documents\\Julia_Code\\Learning\\MyFile_Horizontal.txt",',');

  x = a+c

  y = b+c

  println(x,", ",y)

 writedlm("C:\\Users\\peter\\Documents\\Julia_Code\\Learning\\delim_file.txt",[x y],',')

I changed your program a little to reflect intermediate actions. It seems to work. If I wanted to print a header x, y on the output file what is the best way? If I use header = true then the headings become column1 and column 2 not x and y. Thanks Peter


 using DataFrames

 df = CSV.read("C:\\Users\\peter\\Documents\\Julia_Code\\Learning\\MyFile_Horizontal.txt", DataFrame, header=[:a, :b, :c, :d])

 a, b, c, d = eachcol(df);

  x= a + d 

  y=c + d

 CSV.write("C:\\Users\\peter\\Documents\\Julia_Code\\Learning\\out.csv",Tables.table([x y]),header=false)

Thanks, much nicer!

You can set header = ["x", "y"] for this:

julia> using Tables, CSV

julia> CSV.write("file.csv", Tables.table([1 2]), header=["x", "y"])
"file.csv"

julia> print(read("file.csv", String))
x,y
1,2

Personally I think that, if you can choose your file format you might not want CSV if this is a configuration file with a header and never more than one row of values.

Instead, an ini-like format like TOML is preferable to a tabular format for configuration because it’s more flexible. For example to create some configuration and write it to the file:

julia> using TOML

julia> config = Dict("x"=>1, "y"=>2, "z"=>[4,5,6])
Dict{String,Any} with 3 entries:
  "x" => 1
  "z" => [4, 5, 6]
  "y" => 2

julia> open(io->TOML.print(io, config), "config.toml", write=true)

Here’s the content of the generated β€œconfig.toml”

julia> print(read("config.toml", String))
x = 1
z = [4, 5, 6]
y = 2

Now you can read it back in and refer to the variables by name, relative to the config Dict:

julia> config_read = open(io->TOML.parse(io), "config.toml")
Dict{String,Any} with 3 entries:
  "x" => 1
  "z" => [4, 5, 6]
  "y" => 2

julia> config_read["x"]
1

julia> config_read["y"]
2

julia> config_read["z"]
3-element Array{Int64,1}:
 4
 5
 6

Notice how this allows a lot of flexibility in having nested structures of Dicts and Arrays for your program configuration.

2 Likes