Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a Python script that reads large data files that is over a thousand files with each of these files containing thousands of data

I have a Python script that reads large data files that is over a thousand files with each of these files containing thousands of data points; the thing is, I do not need all the data since it contains * or words that will give me a ValueError. The rows I would like to skip are the rows in which there is no data. So, is there a Python function that allows me to skip rows of no data in a data file in a sequence, then continue reading the file? What lines of code must I modify to achieve this?

Posted below is the code I used to read the files, along with an example of the data file I am reading from. You will notice that there are an * and some words for the rows, those are not needed, as they are giving me a ValueError function and must be skipped over as the error that it reads out is: image text in transcribed

atom_number.append(int(line[0])) ValueError: invalid literal for int() with base 10: As such, this is the code I am using files=glob.glob("*_21.dat') files.sort() for file in files: f=open(file, 'r') # open the current file abundance=f.readlines() # read it #print('file', file) # To append the atom. baryon, and mass fraction to empty lists atom_number=[] # to append the atomic number from the abundance files baryon_number=[] # to append the baryon number from the abundance files mass_fraction=[] # to append the mass fraction from the abundance files # this loop to read the columns from the abundance file for win abundance: line :w.split() atom_number.append(int(line[@])) baryon_number.append(int(line[1])) mass_fraction.append(float(line[2])) And this is a sample of the many * _21.dat files I am reading, and you can see as a started earlier, 1 need to know how to skip three rows, read the data, and repeat in a sequence. trho_hist: mass interior = 0.000E+00 shell mass = time b9 b10 1 4.797030000000E-05 9.000000E-30 1.000000E-29 0.000E+00 b11 1.100000E-29 c11 1.100000E-29 C 5.00 trho_hist: mas mass interior = 0.000E+00 time b9 101 6.993032347891E-02 9.083043E-23 shell mass = b10 2.079933E-15 @.000E+00 b11 2.610734E-13 n c11 2.697459E-14 4.89 trho_hist: mass interior = 0.000E+00 time b9 201 7.039261757564E-02 6.396873E-15 shell mass = b10 7.006367E-17 0.000E+00 b11 7.696272E-15 c11 3.277727E-09 C 2.75 * trho_hist: mass interior = 0.000E+00 time b9 301 7.088481182816E-02 4.591306E-12 shell mass = b10 5.769626E-16 @.000E+00 b11 1.187112E-15 c11 1.667039E-10 C 1.52 0.000E+00 trho_hist: mass interior = 0.000E+00 time b9 401 7.134944609308E-02 2.557561E-11 shell mass = b10 1.978589E-13 b11 c11 1.197408E-10 C 2.96 2.380623E-14

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Management Databases And Organizations

Authors: Richard T. Watson

6th Edition

1943153035, 978-1943153039

More Books

Students also viewed these Databases questions