# Convert string in text file into int

I have the following textfile:

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

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

How does the first line even work? For me

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

``````using TOML
data = TOML.parse( replace( textfile , "(" => "[", ")" => "]") )
``````
1 Like

Another way using regex:

``````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])]
``````
2 Likes
``````
twins=Iterators.partition(split(filter(x->isdigit(x)||x==',',  lines[3]),','),2)

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

``````

#or

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