我试图使我的脚本执行基于一个网站上的表行值的存在特定的动作。例如,如果x在表'lab'的第一行,创建调查,否则移动到下一行,检查x是否在该行。对不起,我正在尝试这个网站是无法访问的那些谁没有帐户,但请看到我的代码的简单版本,以帮助弄清楚这一点。到目前为止,我被困在第二个for循环上,下面的代码遍历每一行并打印出来,但只是挂起。也许这只是我需要的休息,但我已经试过了(休息,继续,通过)。
#for each patient id in list, find the section in the patients web-table by looking through each row where the covid name and result is stored
#search for results table within a table in a web page for eicrs
elems = driver.find_elements_by_xpath('//*[@id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
continue #continue loop through rows
continue #not sure if needed
break #break out of topmost for loop and move to next line of code once value has been found to match condition.
print('done with that')
如果我正确理解你需要什么,你应该从你的代码中删除continue
和底部的break
,并在if
和else
块内添加break
,所以如果你发现了你正在寻找的条件并执行了你需要的操作-现在打破for循环。
像这样:
elems = driver.find_elements_by_xpath('//*[@id="xmlBlock"]/ul[1]/li/table[1]/tbody')
for idx, elem in enumerate(elems, 1):
for rownum, lab in enumerate(elem.text.split('n'), 1):
#get lab test from first column for each row
lab_test = text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[1]')
#get result from second column for each row
lab_result= text_xpath(f'//*[@id="xmlBlock"]/ul[1]/li[{idx}]/table[1]/tbody/tr[{rownum}]/td[2]')
#if lab test is in list of rna tests and positive lab regex, create CONFIRMED investigation
if re.search(covid_test_names, lab_test.lower()) and re.search(pos_regex, lab_result.lower()):
print('Log update: created confirmed investigation')
break
#else if lab test is in list of antigen tests and positive lab regex, create PROBABLE investigation
elif re.search(ant_regex, lab_test.lower()) and re.search(antigen_pos_regex, lab_result.lower()):
print('Log update: created probable investigation')
break
else:
print('Log doc: No lab test matches regex', lab_test, lab_result)
has been found to match condition.
print('done with that')
但也许我没有理解你的逻辑和问题