variables - Extraction via string methods in python -


i have read code extraction of data website through string methods:

def extract_results(data)      start_index= data.find("<p>")      while -1 != start_index:          end_index = data.find("</p>", start_index) 

here while loop doing? why start_index being compared -1?

the return value of str.find() -1 if text not found:

str.find(sub[, start[, end]])
return lowest index in string substring sub found, such sub contained in slice s[start:end]. optional arguments start , end interpreted in slice notation. return -1 if sub not found.

the while loop makes code go endless loop if start_index not -1 , useless unless there more code following snippet shared us.

presumably there return data[start_index + 3:end_index] next line, in case using if start_index > -1: instead of while statement have been far more readable.

it could start_index set again further down, of course.


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -