# Convert string in text file into int

**URL:** <https://discourse.julialang.org/t/convert-string-in-text-file-into-int/89663>\
**Category:** General Usage\
**Tags:** regex, io\
**Created:** [November 2, 2022, 2:50pm UTC](https://discourse.julialang.org/t/convert-string-in-text-file-into-int/89663 "2022-11-02T14:50:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ahmad88](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmad88/32/32437_2.png) [@ahmad88](https://discourse.julialang.org/u/ahmad88)\
**Post date:** [November 2, 2022, 2:50pm UTC](https://discourse.julialang.org/t/convert-string-in-text-file-into-int/89663/1 "2022-11-02T14:50:02Z")

</div>

I have the following textfile:

```julia
initial=0 
n=39
E=[( 0 , 8 ), ( 0 , 13 ), ( 0 , 20 ), ( 0 , 22 ), ( 0 , 26 ), ( 0 , 36 ), ( 0 , 5 ), ( 0 , 10 ), ( 0 , 17 ), ( 0 , 27 ), ( 1 , 18 ), ( 2 , 5 ), ( 2 , 8 ), ( 3 , 4 ), ( 3 , 21 ), ( 3 , 25 ), ( 3 , 32 ), ( 3 , 5 ), ( 3 , 20 ), ( 3 , 24 ), ( 5 , 10 ), ( 5 , 11 ), ( 5 , 17 ), ( 5 , 19 ), ( 5 , 22 ), ( 5 , 23 ), ( 5 , 24 ), ( 5 , 27 ), ( 5 , 28 ), ( 5 , 32 ), ( 5 , 33 ), ( 5 , 36 ), ( 5 , 8 ), ( 5 , 14 ), ( 5 , 15 ), ( 5 , 20 ), ( 5 , 21 ), ( 5 , 25 ), ( 5 , 26 ), ( 5 , 35 ), ( 5 , 37 ), ( 5 , 38 ), ( 6 , 12 ), ( 6 , 16 ), ( 6 , 30 ), ( 6 , 31 ), ( 6 , 34 ), ( 6 , 25 ), ( 7 , 29 ), ( 7 , 36 ), ( 8 , 10 ), ( 8 , 20 ), ( 8 , 26 ), ( 8 , 36 ), ( 8 , 27 ), ( 8 , 38 ), ( 9 , 17 ), ( 9 , 19 ), ( 9 , 22 ), ( 10 , 17 ), ( 10 , 26 ), ( 10 , 20 ), ( 10 , 27 ), ( 12 , 16 ), ( 12 , 25 ), ( 12 , 30 ), ( 12 , 31 ), ( 12 , 34 ), ( 13 , 22 ), ( 14 , 15 ), ( 14 , 26 ), ( 16 , 30 ), ( 16 , 31 ), ( 16 , 34 ), ( 16 , 25 ), ( 17 , 19 ), ( 17 , 26 ), ( 17 , 36 ), ( 17 , 20 ), ( 17 , 22 ), ( 17 , 27 ), ( 18 , 22 ), ( 18 , 28 ), ( 19 , 20 ), ( 19 , 22 ), ( 19 , 28 ), ( 20 , 24 ), ( 20 , 26 ), ( 20 , 27 ), ( 20 , 32 ), ( 20 , 21 ), ( 20 , 22 ), ( 20 , 25 ), ( 20 , 28 ), ( 20 , 36 ), ( 20 , 37 ), ( 20 , 38 ), ( 21 , 25 ), ( 22 , 28 ), ( 25 , 30 ), ( 25 , 34 ), ( 25 , 31 ), ( 26 , 27 ), ( 27 , 36 ), ( 29 , 36 ), ( 30 , 34 ), ( 30 , 31 ), ( 31 , 34 )]

```

what I am trying to do is to assign these values as follows

```julia
lines = [line for (i, line) in enumerate(eachline("textfile.txt"))]
a=parse(Int64,lines[1])
b=parse(Int64,lines[2])
c=parsetuple(lines[3]::String) = Tuple(Int64(x) for x in lines[3])

```

it works for a and b but for c it still deal with it as an integer, what is the problem here ?

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [November 2, 2022, 3:18pm UTC](https://discourse.julialang.org/t/convert-string-in-text-file-into-int/89663/2 "2022-11-02T15:18:29Z")

</div>

How does the first line even work? For me

```julia
parse( Int64, "initial=0")

```

errors. (e.g. `invalid base 10 digit 'i' in "initial=0"`)

* * *

Have you considered to maybe just call `include("textfile.txt")`? Then you would have all three variables directly without any manual parsing.

There are maybe better-suited text formats for storing your data, such as .toml for values and short arrays or .csv for tables. If you can change the input to a proper file format with existing parsers, that would probably be the best solution.

Side note: This would also work our of the box, but it gives you a vector of vectors…

```julia
using TOML
textfile = read("textfile.txt", String)
data = TOML.parse( replace( textfile , "(" => "[", ")" => "]") )

```

---

<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:** [November 2, 2022, 6:00pm UTC](https://discourse.julialang.org/t/convert-string-in-text-file-into-int/89663/3 "2022-11-02T18:00:17Z")

</div>

Another way using regex:

```julia
lines = readlines("textfile.txt")
a = parse(Int64, match(r".*=(\d*)", lines[1]).captures[1])
b = parse(Int64, match(r".*=(\d*)", lines[2]).captures[1])
c = [Tuple(parse.(Int, m.captures)) for m in eachmatch(r"\( *(\d+) *, *(\d+) *\)", lines[3])]

```

---

<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:** [November 2, 2022, 7:14pm UTC](https://discourse.julialang.org/t/convert-string-in-text-file-into-int/89663/4 "2022-11-02T19:14:49Z")

</div>

```julia

twins=Iterators.partition(split(filter(x->isdigit(x)||x==',', lines[3]),','),2)

c=[Tuple(parse.(Int, m)) for m in twins]

```

#or

```julia
eval(Meta.parse(lines[3]))

```
