파이썬 텍스트파일에서 텍스트 출력하기

조회수 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()
  • 원하는 결과물 예시도 올려주세요. 초보자 2021.9.24 13:44

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'
    

    코드 올려주신거 보면 충분히 혼자 하실 수 있었을거 같은데 좀 더 시도해보시고 질문하시는게 좋아보여요

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)