# Convert an Array Written as a String to An Actual Array

**URL:** https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013
**Category:** General Usage
**Tags:** strings, array, parsing
**Created:** [May 28, 2021, 7:43pm UTC](https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013 "2021-05-28T19:43:25Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)
#### Post date: [May 28, 2021, 7:43pm UTC](https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013/1 "2021-05-28T19:43:25Z")

</div>

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:

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

```

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

Thanks!

~ tcp 🌳

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [May 28, 2021, 7:49pm UTC](https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013/2 "2021-05-28T19:49:53Z")

</div>

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

```

---

<div class="post-metadata">

### Author: ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)
#### Post date: [May 28, 2021, 7:59pm UTC](https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013/3 "2021-05-28T19:59:23Z")

</div>

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

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

```

---

<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 29, 2021, 6:59pm UTC](https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013/4 "2021-05-29T18:59:17Z")

</div>

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

---

<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 29, 2021, 7:03pm UTC](https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013/5 "2021-05-29T19:03:33Z")

</div>

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

---

<div class="post-metadata">

### Author: ![StatisticalMouse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statisticalmouse/32/43370_2.png) [@StatisticalMouse](https://discourse.julialang.org/u/StatisticalMouse)
#### Post date: [May 29, 2021, 7:03pm UTC](https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013/6 "2021-05-29T19:03:47Z")

</div>

I have a very similar problem. I want to copy a color scheme from [colorbrewer2.org](http://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

```julia
['#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?

---

<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 29, 2021, 7:29pm UTC](https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013/7 "2021-05-29T19:29:58Z")

</div>

@StatisticalMouse, would this work for your use case:

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

```

---

<div class="post-metadata">

### Author: ![StatisticalMouse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/statisticalmouse/32/43370_2.png) [@StatisticalMouse](https://discourse.julialang.org/u/StatisticalMouse)
#### Post date: [May 30, 2021, 8:33am UTC](https://discourse.julialang.org/t/convert-an-array-written-as-a-string-to-an-actual-array/62013/8 "2021-05-30T08:33:06Z")

</div>

It works! This produces the colors in the input:

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

```

 ![Screenshot 2021-05-30 at 11.30.31](https://global.discourse-cdn.com/julialang/original/3X/0/f/0fb20cd837bc480a81b805283576e4584b38a02b.png)
