Convert an Array Written as a String to An Actual Array

Hi all,

I have no idea how this happened, but I am using a dataset where in the file, arrays were actually stored as strings like this:

"[1, 2, 3, 4, 5]"

How do I best convert the above into an actual array of integer values?

Thanks!

~ tcp :deciduous_tree:

julia> str = "[1, 2, 3, 4, 5]";

julia> parse.(Int, split(chop(str; head=1, tail=1), ','))
5-element Vector{Int64}:
 1
 2
 3
 4
 5
6 Likes

This is great - I really like your solution. For completeness, here is another solution I found from user Anand Jain:

julia> @benchmark (Meta.parse($str) |> eval)
BenchmarkTools.Trial:
  memory estimate:  2.58 KiB
  allocs estimate:  40
  --------------
  minimum time:     104.374 μs (0.00% GC)
  median time:      109.657 μs (0.00% GC)
  mean time:        117.397 μs (0.00% GC)
  maximum time:     899.178 μs (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1

And here is the timing of yours @fredrikekre :

julia> @benchmark parse.(Int, split(chop($str; head=1, tail=1), ','))
BenchmarkTools.Trial:
  memory estimate:  304 bytes
  allocs estimate:  3
  --------------
  minimum time:     672.151 ns (0.00% GC)
  median time:      703.967 ns (0.00% GC)
  mean time:        738.335 ns (2.35% GC)
  maximum time:     32.701 μs (97.49% GC)
  --------------
  samples:          10000
  evals/sample:     152
2 Likes

Regex is usually used for this task, for example:
parse.(Int,split(replace(str, r"[^0-9.]"=>" ")))

A variant of @fredrikekre’s solution using strip instead of chop:
parse.(Int,split(strip(str,['[',']']), ","))

I have a very similar problem. I want to copy a color scheme from colorbrewer2.org and convert it to RGB colors.

I can copy&paste the Colors for this scheme as a JS array, which gives one of the following

['#edf8fb','#b2e2e2','#66c2a4','#238b45']
['rgb(237,248,251)','rgb(178,226,226)','rgb(102,194,164)','rgb(35,139,69)']

I know Julia already has a Colorbrewer palette, but the website is much more convenient.

How would I convert this?

@StatisticalMouse, would this work for your use case:

str = "['rgb(237,248,251)','rgb(178,226,226)','rgb(102,194,164)','rgb(35,139,69)']"
s2 = replace.(split(str,"rgb"),r"[^0-9.]"=>" ")[2:end]
[parse.(Int, split(s)) for s in s2]

4-element Vector{Vector{Int64}}:
 [237, 248, 251]
 [178, 226, 226]
 [102, 194, 164]
 [35, 139, 69]
1 Like

It works! This produces the colors in the input:

str = "['rgb(255,255,204)','rgb(255,237,160)','rgb(254,217,118)','rgb(254,178,76)','rgb(253,141,60)','rgb(252,78,42)','rgb(227,26,28)','rgb(189,0,38)','rgb(128,0,38)']"
z = @chain str begin
	replace.(split(_,"rgb"),r"[^0-9.]"=>" ")[2:end]
	[parse.(Int, split(s)) for s in _]
	(c->RGB(c[1]/255,c[2]/255,c[3]/255)).(_)
end

3 Likes