Import text that follows a particular pattern from a Python text file

Asked 2 years ago, Updated 2 years ago, 49 views

I want to get the log file and get the value behind a specific pattern. I want to get the value following detection_val= in the code below, but the error ValueError: 'detection_val=' is not in list keeps coming out What's the problem?

Text file example and code.

I0112 12:04:20.427652  8361 solver.cpp:546]     Test net output #0: detection_eval = 0.0274755…I0112 12:15:39.046685  8361 solver.cpp:546]     Test net output #0: detection_eval = 0.0610784

file = open(r"C:\Users\Sales Support\Desktop\03_parsing_csv_plot\vgg16_recog_107_20210112.log", 'rt', encoding='UTF8')
lines = file.readlines()
for line in lines:
        item = line.split(" ")
        Detection_eval = item[item.index("detection_eval = ")+1]

python text

2022-09-20 15:05

1 Answers

>>> a = '0112 12:04:20.427652 8361 solver.cpp:546] Test net output #0: detection_eval = 0.0274755…'
>>> a = a.split('detection_eval = ')
>>> a[1:]
['0.0274755…']


2022-09-20 15:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.