파이썬 텍스트파일에서 텍스트 출력하기
조회수 443회
텍스트파일에서 Iteration 뒤에 오는 값과 detection_eval뒤에 오는 값을 동시에 출력하고 싶은데요 어떤식으로 표현하면될까요?현재는 detection_eval 뒤에 오는 값만 출력할수있는 상태입니다.
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)
아래는 작성한 전체코드입니다.
<pre>import re
file = open(r"C:\Users\영업지원\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()
-
(•́ ✖ •̀)
알 수 없는 사용자 - 〉
1 답변
-
>>> 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'
코드 올려주신거 보면 충분히 혼자 하실 수 있었을거 같은데 좀 더 시도해보시고 질문하시는게 좋아보여요
댓글 입력