# Reading text or csv files and assigning values to variable names

**URL:** <https://discourse.julialang.org/t/reading-text-or-csv-files-and-assigning-values-to-variable-names/50610>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 23, 2020, 4:05am UTC](https://discourse.julialang.org/t/reading-text-or-csv-files-and-assigning-values-to-variable-names/50610 "2020-11-23T04:05:24Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 24, 2020, 8:27am UTC](https://discourse.julialang.org/t/reading-text-or-csv-files-and-assigning-values-to-variable-names/50610/22 "2020-11-24T08:27:14Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [November 24, 2020, 8:37am UTC](https://discourse.julialang.org/t/reading-text-or-csv-files-and-assigning-values-to-variable-names/50610/23 "2020-11-24T08:37:36Z")

</div>

And using CSV and DataFrames:

```julia
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
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.

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [November 24, 2020, 10:11pm UTC](https://discourse.julialang.org/t/reading-text-or-csv-files-and-assigning-values-to-variable-names/50610/24 "2020-11-24T22:11:31Z")

</div>

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

```nohighlight

 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],',')

```

---

<div class="post-metadata">

**Author:** ![Peter-Jolly](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/peter-jolly/32/19371_2.png) [@Peter-Jolly](https://discourse.julialang.org/u/Peter-Jolly)\
**Post date:** [November 24, 2020, 10:22pm UTC](https://discourse.julialang.org/t/reading-text-or-csv-files-and-assigning-values-to-variable-names/50610/25 "2020-11-24T22:22:05Z")

</div>

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

```nohighlight

 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)

```

---

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [November 25, 2020, 3:12am UTC](https://discourse.julialang.org/t/reading-text-or-csv-files-and-assigning-values-to-variable-names/50610/26 "2020-11-25T03:12:09Z")

</div>

> [@Peter-Jolly](#):
>
> Chris I hope this code is not mangled:

Thanks, much nicer!

> [@Peter-Jolly](#):
>
> If I wanted to print a header x, y on the output file what is the best way?

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

```julia
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
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
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
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.

[Previous page](https://discourse.julialang.org/t/reading-text-or-csv-files-and-assigning-values-to-variable-names/50610.md?page=1)
