Python Forum
For Loop Returning 3 Results When There Should Be 1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
For Loop Returning 3 Results When There Should Be 1
#11
(Sep-26-2021, 04:38 PM)SamHobbs Wrote:
(Sep-26-2021, 11:05 AM)knight2000 Wrote: So I followed your suggestion to use lxml and tried the following code:
In your fixed code you first find relevant div elements then look for a relevant span element and I think the requirement for the div elements was not in the original question. You were saying the code does not determine when there is not a match and I could not understand what that means.

The sample file I provided contained one record where the span class of "w_A w_C w_B mr1 mt1 ph1" did exist and one record where the span class "w_A w_C w_B mr1 mt1 ph1" did not exist.

In my original code, for starters I was trying to do a 'for loop' using soup which is incorrect as snippsat taught me.
Reply
#12
Sorry Sam, I probably wasn't overly clear to address your full question. Confused

The reason I changed the code in the end to include the additional div (unlike the original code), was because snippsat educated me that I shouldn't be using 'soup' in a 'for loop'.

(Sep-27-2021, 02:16 AM)knight2000 Wrote:
(Sep-26-2021, 04:38 PM)SamHobbs Wrote: In your fixed code you first find relevant div elements then look for a relevant span element and I think the requirement for the div elements was not in the original question. You were saying the code does not determine when there is not a match and I could not understand what that means.

The sample file I provided contained one record where the span class of "w_A w_C w_B mr1 mt1 ph1" did exist and one record where the span class "w_A w_C w_B mr1 mt1 ph1" did not exist.

In my original code, for starters I was trying to do a 'for loop' using soup which is incorrect as snippsat taught me.
Reply
#13
(Sep-27-2021, 02:23 AM)knight2000 Wrote: The reason I changed the code in the end to include the additional div (unlike the original code), was because snippsat educated me that I shouldn't be using 'soup' in a 'for loop'.
Previously you said:
(Sep-26-2021, 04:24 AM)knight2000 Wrote: 1 instance where there is no record of the object
But at that point there was no explanation of the requirements for that. The point I am trying to make is that for the future it helps to provide details like that. As best as I understand, the loop you had would have found all items except the one.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 990 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  Help add for loop results in a list paulo79 4 1,488 Mar-09-2022, 05:49 PM
Last Post: deanhystad
  returning values in for loop Nickd12 4 11,838 Dec-17-2020, 03:51 AM
Last Post: snippsat
  Search Results Web results Printing the number of days in a given month and year afefDXCTN 1 2,188 Aug-21-2020, 12:20 PM
Last Post: DeaD_EyE
  Adding loop results as rows in dataframe Shreya10o 2 2,144 May-09-2020, 11:00 AM
Last Post: Shreya10o
  How to append one function1 results to function2 results SriRajesh 5 3,072 Jan-02-2020, 12:11 PM
Last Post: Killertjuh
  Returning true or false in a for loop bbop1232012 3 8,035 Nov-22-2018, 04:44 PM
Last Post: bbop1232012
  RegExp: returning 2nd loop in new document syoung 5 3,819 May-02-2018, 12:36 PM
Last Post: syoung

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020