Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read txt data separated by empty lines as several numpy arrays

I have some data in a txt file as follows:

# Contour 0, label:       37
 41.6  7.5
 41.5  7.4 
 41.5  7.3 
 41.4  7.2 

# Contour 1, label: 
 48.3  2.9 
 48.4  3.0 
 48.6  3.1 

# Contour 2, label: 
 61.4  2.9 
 61.3  3.0 
....

So every block begins with a comment and ends with a blank line. I want to read out those data and bring them into a list which consists of numpy arrays, so like

# list as i want it:
[array([[41.6, 7.5], [41.5, 7.4], [1.5, 7.3], [41.4, 7.2]]),
 array([[48.3, 2.9], [48.4, 3.0], [48.6, 3.1]]),
 array([[61.4, 2.9], [61.3, 3.0]]), ...]

Is there an efficient way to do that with numpy? genfromtxt or loadtxt seems not to have the required options!?

like image 284
Rintisch Avatar asked Oct 27 '25 12:10

Rintisch


2 Answers

Like this?

import numpy as np

text = \
'''
# Contour 0, label:       37
 41.6  7.5
 41.5  7.4 
 41.5  7.3 
 41.4  7.2 

# Contour 1, label: 
 48.3  2.9 
 48.4  3.0 
 48.6  3.1 

# Contour 2, label: 
 61.4  2.9 
 61.3  3.0 
'''
for line in text.split('\n'):
    if line != '' and not line.startswith('#'):
        data = line.strip().split()
        array = np.array([float(d) for d in data])
        print(array)
like image 72
alec_djinn Avatar answered Oct 30 '25 01:10

alec_djinn


You could use Python's groupby function to group the 3 entries together as follows:

from itertools import groupby
import numpy as np

array_list = []

with open('data.txt') as f_data:    
    for k, g in groupby(f_data, lambda x: x.startswith('#')):
        if not k:
            array_list.append(np.array([[float(x) for x in d.split()] for d in g if len(d.strip())]))

for entry in array_list:
    print entry
    print

This would display the array_list as follows:

[[ 41.6   7.5]
 [ 41.5   7.4]
 [ 41.5   7.3]
 [ 41.4   7.2]]

[[ 48.3   2.9]
 [ 48.4   3. ]
 [ 48.6   3.1]]

[[ 61.4   2.9]
 [ 61.3   3. ]]
like image 27
Martin Evans Avatar answered Oct 30 '25 02:10

Martin Evans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!