0

I am having a hard time figuring out how to create multiple arrays from data contained within a single text file using numpy.

Here is an example of such data text file for example. Here is a collection of temperature monitoring experiment vs. time, in 3 different rooms (Note that not all three rooms have equal number of data points. I.E Room 1 has 50 measurements and Room 3 has 60):

 ****************************************
 # Room 1 Temperature Data
 ****************************************
 Time(min)    Temperature (F)
 0.000000     79.437828
 1.000000     79.486399  
 2.000000     78.937826  
 3.000000     79.915109  
 4.000000     79.783212  
 5.000000     80.439590  
 6.000000     80.584616  
 7.000000     80.888416  
 8.000000     81.347893  
 9.000000     81.130460  
 10.000000    80.916185  
 11.000000    81.635287  
 12.000000    82.255327  
 13.000000    82.928414  
 14.000000    82.304390  
 15.000000    82.023584  
 16.000000    81.987552  
 17.000000    82.919697  
 18.000000    83.046323  
 19.000000    83.701333  
 20.000000    82.853548  
 21.000000    82.959824  
 22.000000    83.055705  
 23.000000    83.866592  
 24.000000    83.605570  
 25.000000    84.407895  
 26.000000    83.689157  
 27.000000    82.887504  
 28.000000    83.833871  
 29.000000    84.717961  
 30.000000    84.899855  
 31.000000    84.003771  
 32.000000    83.104074  
 33.000000    82.753278  
 34.000000    82.504778  
 35.000000    83.033076  
 36.000000    83.751608  
 37.000000    83.843025  
 38.000000    83.490735  
 49.000000    83.302364  
 50.000000    82.521700   
****************************************
# Room 2 Temperature Data
****************************************
 Time(min)    Temperature (F)
 0.000000     75.782390  
 1.000000     75.320445  
 2.000000     74.922264  
 3.000000     75.172929  
 4.000000     75.732617  
 5.000000     75.574824  
 6.000000     75.888783  
 7.000000     75.736023  
 8.000000     75.439475  
 9.000000     75.688731  
 10.000000    76.311728  
 11.000000    76.299164  
 12.000000    75.405466  
 13.000000    75.371734  
 14.000000    75.381002  
 15.000000    75.254838  
 16.000000    75.006870  
 17.000000    75.562626  
 18.000000    74.762781  
 19.000000    73.795740  
 20.000000    74.360492  
 21.000000    75.066809  
 22.000000    74.514213  
 23.000000    74.768058  
 24.000000    73.897399  
 25.000000    73.375240  
 26.000000    73.294786  
 27.000000    73.620151  
 28.000000    73.055555  
 29.000000    72.995449  
 30.000000    73.952031  
 31.000000    73.978138  
 32.000000    73.918401  
 33.000000    74.250683  
 34.000000    74.293124  
 35.000000    73.807662  
 36.000000    74.455998  
 37.000000    73.713568  
 38.000000    73.057456  
 39.000000    72.407279  
 40.000000    72.171160   

****************************************
# Room 3 Temperature Data
****************************************
 Time(min)    Temperature (F) 
 0.000000    70.723365  
 1.000000    71.680261  
 2.000000    70.688760  
 3.000000    70.808473  
 4.000000    71.312032  
 5.000000    71.092122  
 6.000000    70.709332  
 7.000000    71.304622  
 8.000000    71.750163  
 9.000000    72.351945  
 10.000000    73.295310  
 11.000000    74.050310  
 12.000000    74.263993  
 13.000000    73.812078  
 14.000000    73.849626  
 15.000000    73.987411  
 16.000000    74.891099  
 17.000000    74.779985  
 18.000000    75.671037  
 19.000000    75.816610  
 20.000000    76.651356  
 21.000000    76.419316  
 22.000000    75.513236  
 23.000000    76.234259  
 24.000000    76.728721  
 25.000000    76.832285  
 26.000000    77.067110  
 27.000000    77.743246  
 28.000000    78.542883  
 29.000000    78.436506  
 30.000000    77.641707  
 31.000000    78.297866  
 32.000000    79.028430  
 33.000000    78.522984  
 34.000000    78.421532  
 35.000000    78.708468  
 36.000000    78.463713  
 37.000000    78.078904  
 38.000000    78.865942  
 39.000000    79.072956  
 40.000000    79.827616  
 41.000000    78.946210  
 42.000000    79.194117  
 43.000000    79.871765  
 44.000000    80.085492  
 45.000000    80.440528  
 46.000000    80.836500  
 47.000000    81.725440  
 48.000000    82.491217  
 49.000000    81.830982  
 50.000000    82.190160  
 51.000000    81.254962  
 52.000000    81.835875  
 53.000000    82.459993  
 54.000000    81.881924  
 55.000000    81.921508  
 56.000000    82.496043  
 57.000000    82.507862  
 58.000000    83.490328  
 59.000000    83.438183  
 60.000000    83.351193

Essentially, I would like to write a parser that can take the data from a textfile (shown above) and reads it into a separate array (for each of the 3 rooms, based on above example), and then assigns these arrays into a dictionary.

Thanks for your help!

1
  • Read the file with ordinary Python text reads, and collect the relevant numbers in a list(s) of lists. Make an array from that. You can postpone the string to float conversion to the np.array(alist, float) step. Commented Feb 11, 2021 at 7:15

1 Answer 1

1

In the code below data is a string with the measurements formatted as in the question. It produces a dictionary of numpy arrays where keys are room numbers.

import re
import numpy as np

match = re.findall(r'[0-9][0-9. \n]*', data)
arrays = {
    int(match[i]): np.fromstring(m[i + 1], sep=" ").reshape(-1, 2)
    for i in range(0, len(match), 2)
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.