Reputation: 285
I have an html file which I open in notepad and read in python as list
lst = file.readlines()
the html is done like this:
<span style="color: #000000; font-weight: bold; ">data 27 dic 2022 - 14:25:59::</span><br>
<br>
<span style="color: #0000ff; ">something</span> -> ALTO<br>
<br>
<span style="color: #000000; font-weight: bold; ">data 27 dic 2022 - 14:38:38::</span><br>
<br>
<span style="color: #0000ff; ">something</span> -> NO_ACQ<br>
<br>
<span style="color: #000000; font-weight: bold; ">data 27 dic 2022 - 14:38:40::</span><br>
I'm making a search on word "data" and from there I manipulate the string in order to get the data. Since I need to iterate this research and I don't want to start everytime from the first "data"/"time"-row, is there a command to get the "index" of the row?
This is the main code:
while exit==0:
timestamp, stop, index=timestamp_search(path,"data",iterazione)
print ('timestamp=', timestamp)
if timestamp>passed_time and timestamp<actual_time:
do something
exit=1
elif stop==1:
exit=1
else:
iterazione=index+1
this instead is the def:
def timestamp_search(file_path, word_1, iterazione):
stop=0
with open(file_path, 'r') as file:
lst = file.readlines()
print ('iterazione',iterazione)
if (lst):
if iterazione<len(lst):
for index, line in enumerate(lst[iterazione:]):
if line.find(word_1) != -1:
print (index,line)
#do something to calculate timestamp
break
else:
stop=1
return timestamp, stop
else:
print ('File vuoto')
return timestamp, stop, index
I've located the error. Basically it is how I'm passing the information to lst. I mean: when iterazione=index=0 in the first cycle, lst starts from index 0 and this is ok when iterazione=index+1 (in the next cycle) this time lst will start from index 1 and the word "data" will be found at index 3. Then again in the next cycle iterazione=4 but this time lst will start from index 4 and this means that "data" will be found at index 0 so in the next loop the counter is wrong.
Upvotes: 0
Views: 56
Reputation: 102
Use the enumerate
method to get an additional index attribute while looping.
for index, line in enumerate(lst[iterazione:]):
if line.find("data") != -1:
print (index, line)
Upvotes: 3