Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading columns between empty lines

Tags:

python

Say the data in file is formatted like so:

1 4 5
2 3 4
4 7 1

1 1 1
2 1 2
3 3 3
4 1 4

2 2 2

and I always want to read portions of the data between empty lines, e.g. I want the columns between the first empty line and the second empty line, so v1 = [1,2,3,4], v2 = [1,1,3,1] and so on. First thing I do I find the indices of where the empty lines occur by:

filetmp = open('data.txt')
indices = []
for i, line in enumerate(filetmp):
    tmp = ''.join(c for c in line if c.isalnum() or c.isspace())
    print tmp
    if not tmp.strip(): indices.append(i)

Now indices indeed contains the right indices, i.e. of empty lines. Next part, is to read the wanted parts, given the indices of empty lines, so that we can fill v1, v2 etc. Should I do this by doing a filetmp.readlines() first? or are there more straightforward ways of reading specific parts, when dealing with columns of data?


1 Answers

I'd this as follow:

with open('data.txt') as f:
    data = f.read()

v = []
# Split the string into blocks, by looking for duplicated line terminaters ('\n\n').
for i, block in enumerate(data.split('\n\n')):
    # Split the blocks in lines by looking for line terminaters ('\n').
    lines = block.split('\n')
    v.append([])
    for line in lines:
        if line == "":
            continue
        v[i] += [line.split(' ')]

# Take the middle block and transpose it.
v1 = map(list, zip(*v[1]))

Of course you can only work with the second block instead of iterating over all.

As a function:

def get_block_from_file(file_path, block_number):
    with open(file_path) as f:
        data = f.read()

    blocks = data.split('\n\n')
    try:
        block = blocks[block_number - 1]
    except IndexError:
        print('Not enough blocks')
        import sys; sys.exit(1)
    v = []
    lines = block.split('\n')
    for line in lines:
        if line == "":
            continue
        v += [map(int, line.split(' '))]

    return map(list, zip(*v))

print(get_block_from_file('data.txt', 2))
like image 195
Jan Zeiseweis Avatar answered Aug 09 '26 16:08

Jan Zeiseweis