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 slices[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
Post a Comment