# Reading data in Julia 1.2.0

**URL:** <https://discourse.julialang.org/t/reading-data-in-julia-1-2-0/45876>\
**Category:** Performance\
**Created:** [September 1, 2020, 6:39am UTC](https://discourse.julialang.org/t/reading-data-in-julia-1-2-0/45876 "2020-09-01T06:39:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nova](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@Nova](https://discourse.julialang.org/u/Nova)\
**Post date:** [September 1, 2020, 6:39am UTC](https://discourse.julialang.org/t/reading-data-in-julia-1-2-0/45876/1 "2020-09-01T06:39:21Z")

</div>

I have a file (A.nets) storing data in the following format:

```julia

NetDegree : 4 n0
	o197239	I : -0.500000	-6.000000
	o197110	O : -1.500000	-3.000000
	o85644	I : -6.000000	-2.000000
	o0	I : -3.000000	-5.000000
NetDegree : 2 n1
	o203752	O : -6.500000	2.000000
	o0	I : -4.000000	-5.000000
NetDegree : 2 n2
	o23	I : -14.500000	0.000000
	o0	O : 0.000000	-1.000000

```

How can I read this file?  
I want the output to be a text file like:

```julia
197239 197110 85644 0
203752 0
23 0
.
.
.

```

---

<div class="post-metadata">

**Author:** ![danielw2904](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielw2904/32/10890_2.png) [@danielw2904](https://discourse.julialang.org/u/danielw2904)\
**Post date:** [September 1, 2020, 7:55am UTC](https://discourse.julialang.org/t/reading-data-in-julia-1-2-0/45876/2 "2020-09-01T07:55:24Z")

</div>

I am not familiar with this data format but if there is not a package to read it automatically you could read it into a vector of lines using `readlines` and then parse it using regex.

Here is an example implementation where I copied your example into A.nets.

```julia
lines = readlines("A.nets")
isskipline(line) = startswith(line, "NetDegree")
readncols(line) = parse(Int, match(r"(?<=\: )\d+", line).match)
readcol(line) = parse(Int, match(r"(?<=\to)\d+", line).match)

function makerow(lines, index, ncols)
    out = Vector{Int}(undef, ncols)
    for (i, j) in enumerate(index:(index+ncols-1))
        out[i] = readcol(lines[j])
    end
    return join(out, ' ')
end

function parsenets(lines)
    index = 1 
    out = String[]
    while index < length(lines)
        if isskipline(lines[index])
            ncols = readncols(lines[index])
            index += 1
            push!(out, makerow(lines, index, ncols)) 
            index += ncols
        else
            index += 1
        end
    end
    return out
end

parsenets(lines)

open("outfile.txt", "w") do io
    for line in parsenets(lines)
        print(io, line)
        print(io, '\n')
    end
end

```
