# How can I read back a delimited file with mixed column types involving vectors

**URL:** https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082
**Category:** General Usage
**Tags:** question, csv, io
**Created:** [May 15, 2022, 2:18am UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082 "2022-05-15T02:18:37Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [May 15, 2022, 2:18am UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/1 "2022-05-15T02:18:37Z")

</div>

Say I have data in the following format

```julia
v = [(1, 0.1, [1, 2, 3]), (2, 0.2, [3,4,5])]

```

I can write it to a delimited file using

```julia
using DelimitedFiles
open("test.csv", w) do io
     writedlm(io, v)
end

```

The content of test.csv is

```julia
1	0.1	[1, 2, 3]
2	0.2	[3, 4, 5]

```

I tried the following way to read it back:

```julia
w = open("test.csv", "r") do io
    readdlm(io)
end

```

However, it splits the vector into parts of strings.

With CSV.jl, it also errored:

```julia
using CSV
CSV.File("test.csv", types=[Int, Float64, Vector{Int}]

```

Anyone has a better idea other than breaking the vector manually before saving?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 15, 2022, 6:17am UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/2 "2022-05-15T06:17:47Z")

</div>

You need to use a file format which supports reading and writing vectors, not a plain text file.

Same discussion was had here: [DataFrames/CSV: how to read vectors from \*.csv?](https://discourse.julialang.org/t/dataframes-csv-how-to-read-vectors-from-csv/57883)

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [May 15, 2022, 9:34am UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/3 "2022-05-15T09:34:03Z")

</div>

I am aware of that discussion after I posted this. However, I don’t want to add those heavy dependency packages such DataFrames.jl. I guess I will simply extract all elements and encoded the vector length in another column. Thanks!

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [May 15, 2022, 1:38pm UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/4 "2022-05-15T13:38:43Z")

</div>

one possible way to do this.

```julia
map( r-> (@. eval(Meta.parse(*(w[r,end-2:end]...)))), axes(w,1))

```

Although I have no idea how inefficient it is.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [May 15, 2022, 2:39pm UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/5 "2022-05-15T14:39:04Z")

</div>

> [@liuyxpp](#):
>
> `1	0.1	[1, 2, 3]`

For each line, replace commas and braces by blanks, then split the data.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [May 15, 2022, 3:24pm UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/6 "2022-05-15T15:24:24Z")

</div>

This is not going to win any coding prize but, fwiw:

```julia
m = []
open("test.csv", "r") do io
    while !eof(io)
       push!(m, eval(Meta.parse(readline(io))))
    end
end
m
2-element Vector{Any}:
 (1, 0.1, [1, 2, 3])
 (2, 0.2, [3, 4, 5])

m == v # true

```

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [May 16, 2022, 1:14am UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/7 "2022-05-16T01:14:41Z")

</div>

Wow, that means I can even parse more complex tuples like `(1, 0.1, [1, 2, 3], [0.1, 0.2, 0.3, 0.4, 0.5, 0.6]) ` which is very close to my actual use case. Thanks!

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [May 16, 2022, 8:41am UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/8 "2022-05-16T08:41:25Z")

</div>

I too had thought of a solution “close” to the one proposed by @rafael.guerra

```julia
map( r-> (@. eval(Meta.parse(*(w[r,end-2:end]...)))), axes(w,1))

arr=[]
open("arrayinfile.csv", "r") do io
    while !eof(io)
        w = readline(io)
        push!(arr,eval(Meta.parse(match(r"\[(.*?)\]", w).match)))
    end
end

```

Note that this (the solution proposed by @rafael.guerra ) works if you use a **comma** and not a space **as the delimiter** when saving the file, as is the default.  
Note incidentally in this regard that the documentation is misleading as it describes a kwarg delim = which is not actually defined.

```julia
julia> using DelimitedFiles

help?> writedlm
search: writedlm

  writedlm(f, A, delim='\t'; opts)

  Write A (a vector, matrix, or an iterable collection of      
  iterable rows) as text to f (either a filename string or an  
  IO stream) using the given delimiter delim (which defaults   
  to tab, but can be any printable Julia object, typically a   
  Char or AbstractString).

```

the syntax that works is the following:

```julia
open("arrayinfile.csv", "w") do io
   writedlm(io, v, ',')
end

```

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [May 16, 2022, 10:22am UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/9 "2022-05-16T10:22:15Z")

</div>

Sometimes I want to read the CSV by eye, so I choose “\t” for the delimiter. And I come up the following solution in the end which works as expected.

```julia
trace = []
    open(trace_file, "r") do io
        readline(io) # skip the header line
        while !eof(io)
            line = replace(readline(io), "\t"=>",");
            push!(trace, eval(Meta.parse(line)))
        end
    end

```

---

<div class="post-metadata">

### Author: ![suavesito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/suavesito/32/34386_2.png) [@suavesito](https://discourse.julialang.org/u/suavesito)
#### Post date: [May 16, 2022, 1:47pm UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/10 "2022-05-16T13:47:50Z")

</div>

You should know that this approach is not really that fast. 😅

On my computer I get

```julia
julia> v = [(rand(Int), rand(), rand(Int64, 3)) for _ in 1:1000];

julia> open("test.csv", "w") do io
            writedlm(io, v, ',')
       end

julia> function open_csv()
           m = Vector{Tuple{Int, Float64, Vector{Int}}}()
           open("test.csv", "r") do io
               while !eof(io)
                   push!(m, eval(Meta.parse(readline(io))))
               end
           end
           m
       end
open_csv (generic function with 1 method)

julia> open_csv(); # compile it

julia> @time @eval w = open_csv();
  0.273463 seconds (54.11 k allocations: 2.600 MiB)

julia> w == v
true

```

So just over 0.2 seconds for a fairly simple expression repeated just 1000 times. If you need to save this data and retrieved in bigger amounts, maybe you should consider another approach to save it in binary format.

Or in another free style CSV format, like specifying number of arrays, later number of elements and then the elements themselves, just for bringing an example. 😄

---

<div class="post-metadata">

### Author: ![liuyxpp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liuyxpp/32/9870_2.png) [@liuyxpp](https://discourse.julialang.org/u/liuyxpp)
#### Post date: [May 16, 2022, 1:51pm UTC](https://discourse.julialang.org/t/how-can-i-read-back-a-delimited-file-with-mixed-column-types-involving-vectors/81082/11 "2022-05-16T13:51:04Z")

</div>

Thanks for benchmarking this. The time is totally fine with me 🙂
