# How to read in a list of integers into an array

**URL:** <https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185>\
**Category:** Statistics\
**Created:** [February 26, 2020, 6:06pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185 "2020-02-26T18:06:49Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 26, 2020, 6:06pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/1 "2020-02-26T18:06:49Z")

</div>

I have been a number of files, each contains one list of integers. I would just like to read in the contents into an array in Julia. Here is an example file:

[http://paste.ubuntu.com/p/pxmwPXkNNk/](http://paste.ubuntu.com/p/pxmwPXkNNk/)

How can I do that?

I tried:

```julia
using DelimitedFiles
numbers = readdlm("test.txt")

```

but that gives something that isn’t right at all.

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [February 26, 2020, 6:23pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/2 "2020-02-26T18:23:15Z")

</div>

As is, `numbers = include("test.txt")` works.

---

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 26, 2020, 6:40pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/3 "2020-02-26T18:40:32Z")

</div>

Wow. Thanks!

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 26, 2020, 6:47pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/4 "2020-02-26T18:47:00Z")

</div>

No seriously, don’t do that.

What is that format supposed to be? How is it produced?

If you know what format it is supposed to be in then use a reader for that format. If you don’t know then it seems to be json compatible and I’ll just read it as json. In any case, never use include for this.

---

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 26, 2020, 7:08pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/5 "2020-02-26T19:08:55Z")

</div>

The lists look exactly like those produced from Python but I think they in fact came from the output of a piece of software called Gap. In any case, I need to read in the files as they are given to me. The main problem is just the carriage returns I think.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [February 26, 2020, 7:22pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/6 "2020-02-26T19:22:12Z")

</div>

> [@lesshaste](#):
>
> The main problem is just the carriage returns I think.

Well, readdlm clearly won’t work, and the new line isn’t the problem. You must first identify what format you are dealing with. If you don’t know or if the producer doesn’t specify, then as I said json should work. In any case, just **don’t use `include`**.

---

<div class="post-metadata">

**Author:** ![Syx\_Pek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/syx_pek/32/6364_2.png) [@Syx\_Pek](https://discourse.julialang.org/u/Syx_Pek)\
**Post date:** [February 26, 2020, 7:27pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/7 "2020-02-26T19:27:19Z")

</div>

> [@yuyichao](#):
>
> Well, readdlm clearly won’t work, and the new line isn’t the problem. You must first identify what format you are dealing with. If you don’t know or if the producer doesn’t specify, then as I said json should work. In any case, just **don’t use `include`**.

I agree with this, even though I proposed the above “solution”. If you’re using this for anything more than a toy, do find a saner way to parse and do input verification.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [February 26, 2020, 8:29pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/9 "2020-02-26T20:29:35Z")

</div>

I think one way in this case is to just input the entire thing as a single string and parse the string yourself. Probably not too bad because you can just split it into chunks by commas, discard the brackets, trim whitespace, and then parse to integer. Some regex wiz can probably do it in 2-3 lines of code.

---

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 26, 2020, 9:17pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/10 "2020-02-26T21:17:24Z")

</div>

I am open to any solution. I think the format is just [int, int, int, …,] with possible extra white spaces and carriage returns.

---

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 26, 2020, 9:20pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/11 "2020-02-26T21:20:15Z")

</div>

I am not sure how I would read it as json. Could you give example code to do that?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 26, 2020, 9:27pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/12 "2020-02-26T21:27:24Z")

</div>

```julia
julia> using JSON

julia> write("data.json", "[1,2,3,4,5]");

julia> JSON.parsefile("data.json")
5-element Array{Any,1}:
 1
 2
 3
 4
 5

```

then convert the vector to whatever numeric type you want.

---

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 26, 2020, 9:35pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/13 "2020-02-26T21:35:08Z")

</div>

Thanks. Now I see that JSON.parsefile can’t cope with the large integers. It gives 0 for all the numbers which are too big. This is even before attempting to convert the vector. This file shows the problem [Ubuntu Pastebin](http://paste.ubuntu.com/p/pxmwPXkNNk/) . Maybe I should ask a separate question for this aspect?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 26, 2020, 9:46pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/14 "2020-02-26T21:46:27Z")

</div>

Edit: Nvm this answer, see answer below.

~~Ah, I didn’t look at the sample file and didn’t notice you needed that big numbers. It probably makes sense to write your own little parser function then like~~

```julia
function read_data(s) 
    joined = join(split(s))
    no_brackets = SubString(joined, nextind(joined, 1), prevind(joined, lastindex(joined)))
    return parse.(BigInt, split(no_brackets, ','))
end

```

```julia
julia> read_data(s)
150-element Array{BigInt,1}:
                                                4
                                               12
                                               32
                                               80
                                              192
                                              448
                                             1024
                                             2304
                                             5120
                                            11264
                                            24576
                                            53248
                                           114688
                                           245760
                                           524288
                                          1114112
                                          2359296
                                          4980736
                                         10485760
                                         22020096
                                         46137344
                                         96468992
                                        201326592
                                        419430400
                                        872415232
                                                ⋮
        10803965149739796214962143785958640713728
        21778071482940061661655974875633165533184
        43896425332801061786775324358698099277824
...

```

---

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 26, 2020, 9:48pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/15 "2020-02-26T21:48:25Z")

</div>

To use that function do I need to read the whole file in as a string somehow first?

---

<div class="post-metadata">

**Author:** ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)\
**Post date:** [February 26, 2020, 11:40pm UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/16 "2020-02-26T23:40:04Z")

</div>

```julia
JSON.parsefile("filename"; inttype=BigInt)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 27, 2020, 7:39am UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/17 "2020-02-27T07:39:40Z")

</div>

Nice!

---

<div class="post-metadata">

**Author:** ![lesshaste](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lesshaste/32/12302_2.png) [@lesshaste](https://discourse.julialang.org/u/lesshaste)\
**Post date:** [February 27, 2020, 10:24am UTC](https://discourse.julialang.org/t/how-to-read-in-a-list-of-integers-into-an-array/35185/18 "2020-02-27T10:24:19Z")

</div>

This is funny that we all missed this! Thank you.
