본문 바로가기
파이썬

파이썬 Python 3.x : 다음 줄로 이동

by º기록 2021. 1. 22.
반응형

.html 파일에서 일부 텍스트를 추출하는 작은 스크립트가 있습니다.

f = open(local_file,"r")
for line in f:
    searchphrase = '<span class="position'
    if searchphrase in line:
        print("found it\n")

그것은 나를 위해 잘 작동합니다 (오류 처리는 나중에 가져올 것입니다), 내 문제는 추출하려는 텍스트가 검색 문구 뒤에 2 줄을 따른다는 것입니다. .html 파일에서 두 줄을 어떻게 아래로 이동할 수 있습니까?

 

해결 방법

 


with open(local_file,"r") as f
    for line in f:
        searchphrase = '<span class="position'
        if searchphrase in line:
            print("found it\n")
            next(f) # skip 1 line
            return next(f)  # and return the line after that.


 

참조 페이지 https://stackoverflow.com/questions/15656817

 

 

반응형

댓글