To output text from a Python text file

Asked 2 years ago, Updated 2 years ago, 37 views

I want to print out the value after Iteration and the value after detection_val in a text file at the same time How can I express it?Currently, only the values that follow the detection_val can be printed.

print("Iteration,detection_eval")
for line in lines:
        if "detection_eval = " in line:
                item = line.split("detection_eval = ")
                Detection_eval = item[1].rstrip('\n')
                print(Detection_eval)

Below is the complete code you created.

<pre>import re
file = open(r"C:\Users\Sales Support\Desktop\03_parsing_csv_plot\vgg16_recog_107_20210112.log", 'rt', encoding='UTF8')
lines = file.readlines()
pt = re.compile(r'Iteration ([\d]+), loss = ([\w.]+)')
fs = pt.findall(str(lines))
print("Iteration,loss")
for f in fs:
        print(int(f[0]), float(f[1]))
file.close()
print("Iteration,detection_eval")
for line in lines:
        if "detection_eval = " in line:
                item = line.split("detection_eval = ")
                Detection_eval = item[1].rstrip('\n')
                print(Detection_eval)
        if "Iteration " in line:
                item = line.split("Iteration ")
                Iteration = item[1].rstrip('\n')
                print(Iteration)
file.close()

python text

2022-09-20 15:03

1 Answers

>>> import re
>>> t = '2021:00:00 detection_eval = 33.33 Iteration 55 loss = 22'
>>> re.compile('detection_eval = (.*?) ').search(t)[1]
'33.33'
>>> re.compile('Iteration (.*?) ').search(t)[1]
'55'
>>> re.compile('loss = (.*)').search(t)[1]
'22'
>>>
>>>
>>> re.compile('detection_eval = (.*?) Iteration (.*?) loss = (.*)').search(t)[1]
'33.33'
>>> re.compile('detection_eval = (.*?) Iteration (.*?) loss = (.*)').search(t)[2]
'55'
>>> re.compile('detection_eval = (.*?) Iteration (.*?) loss = (.*)').search(t)[3]
'22'

I think you could've done it by yourself if you saw the chords It seems better to try more and ask questions


2022-09-20 15:03

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.