简体   繁体   中英

Store a specific value in a line in python

I have a txt output file that I would like to store some of the values in lines.

TBabs*powerlaw   
2 PhoIndex 2.427 3.447  FFFFFFFFF
3 norm 8.454501369e-06 1.387320996e-05  FFFFFFFFF
0p3_1p5abs 1.512967493e-14 1.326152487e-14 1.822308766e-14
0p3_1p5plunabs -13.50750953 -13.21090697  FFFFFFFFF
TBabs(powerlaw + diskbb)
Ftest: 11.9512537 14 11.9512537 12 1

In the above txt file, I would like to store the values of PhoIndex (2.427 and 3.447). I have tried the following:

my_list=[]
filepath = 'output'
with open(filepath) as fp:
   for line in fp:
       if line=="PhoIndex"       
       my_list.append(line)

I do not want to index them based on their order because the output is dynamic and PhoIndex might be in another line. So, the only logical way that I can think of is if some variable is eq to that keyword, store the values in that line. In fact, the most useful thing would be to create an empty dictionary and be able to fill that with values in the output, such as

Before the loop:

source = {
 'model': '',
 'PhoIndex': np.array([]),
 'norm': np.array([]),
 '0p3_1p5abs': np.array([]),
 '0p3_1p5plunabs': np.array([]),
}

After the loop:

source = {
 'model': 'TBabs*powerlaw',
 'PhoIndex': np.array([2.427, 3.447]),
 'norm': np.array([8.454501369e-06, 1.387320996e-05]),
 '0p3_1p5abs': np.array([1.512967493e-14, 1.326152487e-14,1.822308766e-14]),
 '0p3_1p5plunabs': np.array([0p3_1p5plunabs,-13.50750953, -13.21090697]),
}

But I have pretty much no idea how to do that. I appreciate any help regarding any of the issues mentioned above.

With

if line=="PhoIndex"

you're checking if the line is EQUAL to that string. You should rather verify if the checked line CONTAINS that string, by using

if "PhoIndex" in line:

to open your loop. Then, you could transform the line into a list, by separating the string elements on each space, like so:

line_array = line.split(" ")

Then, given that your numbers are always the third and second element of this list if it's the PhoIndex line, you can use

line_array.pop() ## remove the last element

line_array.pop(0)

line_array.pop(0) ## remove the first two elements

And then simply do this for every row, by using the respective string to query for the type of row (as with "PhoIndex") and the corresponding list pops according to the line structures. This works if your txt file always has a regular structure. If not then simply proceed in the same way until the split method, and then just retrieve the elements of the created list you need via characteristics that differentiate them from all the rest, like containing a period character, or whatever.

All left is the definition of an empty template dictionary, as you did above "before the loop", and then assign the lists you obtained as values of their respective dictionary keys. done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM