Reading data in Julia 1.2.0

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


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:

197239 197110 85644 0
203752 0
23 0
.
.
.

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.

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
2 Likes