Mar-18-2020, 09:27 PM
Hello, I'm having trouble recognizing a specific line and reading two lines below it. I would like some help because I've tried many things and couldn't get where I need to.
Well, I have the test.txt file that needs to be read and every time the script finds the string "DOSIMETRY_TOTAL_DOSE" it needs to identify the line and seek the dose value reading that is located two lines below.
Below is the content of my TEST.TXT file
So the script should identify the first string on line 4 and read the dose value on line 6 and write it in a file (extractedlines.txt), then identify another string on line 8, read the value on line 10, and write it again at the end of the extractedlines.txt file. The script should continue till the end of the TEST.TXT file.
My python script is:
When I run it, it reads the TXT, it also identifies the strings and saves it in the extractedlines.txt file but I can't make it skip two lines and read the correct dose value and the result (as seen in my in extractedlines.txt) looks like this:
As you can see here, I can't get the dose values on the TEST.TXT file for each one of the observations.
Can someone help me? I've been racking my brain with this for a few days and I'm getting nowhere.
Thank you very much in advance for any help.
Best
Chuck
Well, I have the test.txt file that needs to be read and every time the script finds the string "DOSIMETRY_TOTAL_DOSE" it needs to identify the line and seek the dose value reading that is located two lines below.
Below is the content of my TEST.TXT file
Quote:TEST.TXT
CHUCK AMARAL, 2020
[DOSIMETRY_TOTAL_DOSE_B: 00]
8.9762
[DOSIMETRY_TOTAL_DOSE_E: 00]
9.7324
[DOSIMETRY_TOTAL_DOSE_B: 01]
20.5469
[DOSIMETRY_TOTAL_DOSE_E: 01]
13.2534
[DOSIMETRY_TOTAL_DOSE_B: 02]
2.2764
[DOSIMETRY_TOTAL_DOSE_E: 02]
7.3634
[DOSIMETRY_TOTAL_DOSE_B: 03]
5.8867
[DOSIMETRY_TOTAL_DOSE_E: 03]
6.2521
So the script should identify the first string on line 4 and read the dose value on line 6 and write it in a file (extractedlines.txt), then identify another string on line 8, read the value on line 10, and write it again at the end of the extractedlines.txt file. The script should continue till the end of the TEST.TXT file.
My python script is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
in_file = "test.txt" out_file = "extractedlines.txt" search_for = "DOSIMETRY_TOTAL_DOSE" line_num = 0 lines_found = 0 with open (out_file, 'w' ) as out_f: with open (in_file, "r" ) as in_f: for line in in_f: line_num + = 1 if search_for in line: lines_found + = 1 print ( "String '{}' found on line {}..." . format (search_for, line_num)) print ( "Dose value: " ) out_f.write(line) out_f.write( 'Dose value: {} \n' ) #HERE I DO NOT KNOW HOW TO MAKE THE SCRIPT READ THE VALUE TWO LINES BELOW print ( "{} lines were found with the string '{}'..." . format (lines_found, search_for)) |
Quote:[DOSIMETRY_TOTAL_DOSE_B: 00]
Dose value: {}
[DOSIMETRY_TOTAL_DOSE_E: 00]
Dose value: {}
[DOSIMETRY_TOTAL_DOSE_B: 01]
Dose value: {}
[DOSIMETRY_TOTAL_DOSE_E: 01]
Dose value: {}
[DOSIMETRY_TOTAL_DOSE_B: 02]
Dose value: {}
[DOSIMETRY_TOTAL_DOSE_E: 02]
Dose value: {}
[DOSIMETRY_TOTAL_DOSE_B: 03]
Dose value: {}
[DOSIMETRY_TOTAL_DOSE_E: 03]
Dose value: {}
As you can see here, I can't get the dose values on the TEST.TXT file for each one of the observations.
Can someone help me? I've been racking my brain with this for a few days and I'm getting nowhere.
Thank you very much in advance for any help.
Best
Chuck