Python Forum
How to read in mulitple files efficiently
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read in mulitple files efficiently
#1
I have multiple files, all containing 10s of thousands of lines of data. I cannot alter the way the data is presented and it appears as seen in the attached screenshot.

The first 2 lines can be ignored; I am only interested in the lines beginning with "hydro".

Currently, my program reads the data in from each file selected as and when I select a graph...

for line in file:
                    if keyword.casefold() in line.casefold():
                        time     = self.convert(line[21:35])                   # Timestep
                        delta_t  = self.convert(line[36:50])                   # Change in timestep
                        mass     = self.convert(line[51:65])                   # Mass of star (excluding envelope)
                        radius   = self.convert(line[66:80])                   # Radius of star (excluding envelope)
                        lum_core = self.convert(line[81:95])                   # Luminosity of star (excluding envelope)
                        lum_tot  = self.convert(line[96:110])                  # Total luminosity (including enveloping cloud)
                        flux     = self.convert(line[111:125])                 # Mass flux
                        ratio    = float(line[125:137])                        # Ratio of star mass against mass of the Sun
                
                        # Store the data into the Numpy array 'data'
                        data = np.append(data, np.array([[time, delta_t, mass, radius, lum_core, lum_tot, flux, ratio]]), axis = 0)
The problem here is that this takes some time (several minutes) and is required each time I create a new graph based upon this data.

I did consider reading the data into a list as and when each file was selected, but this causes a delay between selecting a file and being able to select the next file, as the data is read into the list.

I thought of reading in on a separate thread, but then how do I ensure that the data added to the list is in the correct order?

I would appreciate any and all suggestions on how I might approach this problem.

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
How to read in mulitple files efficiently - by garynewport - Jan-26-2023, 10:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read directory listing of files and parse out the highest number? cubangt 5 2,440 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,433 Feb-13-2022, 07:13 PM
Last Post: menator01
  How to efficiently average same entries of lists in a list xquad 5 2,180 Dec-17-2021, 04:44 PM
Last Post: xquad
  Open and read multiple text files and match words kozaizsvemira 3 6,796 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,880 Dec-06-2020, 04:14 PM
Last Post: Divya577
  How to read csv files parallay Mekala 2 2,018 Oct-24-2020, 07:33 AM
Last Post: Mekala
  Read KML files, edit items, and rewrite files? Winfried 4 4,830 Aug-21-2020, 03:55 PM
Last Post: Winfried
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 4,265 Jul-03-2020, 01:34 AM
Last Post: bowlofred
  Read Multiples Text Files get specific lines based criteria zinho 5 3,181 May-19-2020, 12:30 PM
Last Post: zinho
  getting inputs efficiently arman888 2 1,932 May-19-2020, 05:00 AM
Last Post: pyzyx3qwerty

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020