0

I have a data file that looks like this, and the file type is a list.

############################################################
# Tool
# File: test
#
# mass: mass in GeV
# spectrum: from 1 to 100 GeV
###########################################################
# mass (GeV)   spectrum (1-100 GeV)
10  0.2822771608053263
20  0.8697454394829301
30  1.430461657476815
40  1.9349004472432392
50  2.3876849629827412
60  2.796620869276766
70  3.1726347734996727
80  3.5235401505002244
90  3.8513847250834106
100 4.157478780924807

For me to read the data I would normally have to count how many lines before the first set of numbers and then for loop through the file. In this file its 8 lines

spectrum=[]
mass=[]
with open ('test.in') as m:
        test=m.readlines()
        for i in range(8,len(test)):
            single_line=test[i].split('\t')
            mass.appened(float(single_line[0]))
            spectrum.append(float(single_line[1]))

Let's say I didn't want to open the file to check how many lines there are from the intro statement to the first line of data points. How would I make python automatically start at the first line of data points, and go through the end of the file?

Enrique92
  • 55
  • 6

4 Answers4

0
spectrum=[]
mass=[]
with open ('test.in') as m:
    test=m.readlines()

    for line in test:
        if line[0] == '#':
            continue
        single_line=line.split('\t')
        mass.append(float(single_line[0]))
        spectrum.append(float(single_line[1]))
kamion
  • 461
  • 2
  • 9
  • 1
    you can use `startswith()` and instead of checking for equal check for not equal then append the data – deadshot Jun 23 '20 at 05:55
0

This is a general solution, but should work in your specific case.

you could for each line, check if it starts with a number.

psedo-code

for line in test:
    if line.split()[0].isdigit():
        DoStuffWithData
deadshot
  • 8,881
  • 4
  • 20
  • 39
IkerG
  • 51
  • 6
  • This works great! but print(line) is a string, how could I make this into a list? – Enrique92 Jun 23 '20 at 11:10
  • I don’t get exactly what you mean, but we could make a string into a list like this: the_string.split(“ “) – IkerG Jun 23 '20 at 12:43
0

pseudo code:

for r in m:
    if r.startwith('#'):
        continue
    spt = r.split('\t')
    if len(spt) < 2:
        continue
    ## todo: .....
Olvin Roght
  • 7,677
  • 2
  • 16
  • 35
0

You can filter all lines that start with # by regex or startswith method of string

import re

spectrum=[]
mass=[]
with open ('test.in') as m:
    test= [i for i in f.readlines() if not re.match("^#.*", i)]
    for i in test:
        single_line = i.split('\t')
        mass.appened(float(single_line[0]))
        spectrum.append(float(single_line[1]))

OR

spectrum = []
mass = []
with open('test.in') as m:
    test = [i for i in f.readlines() if not i.startwith("#")]
    for i in test:
        single_line = i.split('\t')
        mass.appened(float(single_line[0]))
        spectrum.append(float(single_line[1]))

This will filter out all the lines that start with #.

Leo Arad
  • 4,452
  • 2
  • 6
  • 17