How to handle skipped items in a for loop

71 views Asked by At

I have been using the Amazon Product API with bottlenose and when iterating over the XML response I have encountered null type errors.

I think I have half managed to handle them however it won't extract the other information if it encounters this error thus showing fewer results than there are.

Is there a way to handle this properly so all the information is extracted and the error is ignored?

price_list = [{}]
    for i in price_search:
      lnp = i.LowestNewPrice.FormattedPrice.text
      qty_n = i.TotalNew.text
      qty_u = i.TotalUsed.text
      int_qty_u = int(qty_u)
    if int_qty_u > 0:
      lup = i.LowestUsedPrice.FormattedPrice.text
    else:
        continue
    price_list.append({'Lowest New Price': lnp, 'Lowest Used Price': lup, 'Quantity New': qty_n, 'Quantity Used': qty_u})

In this instance it is specifically the LowestUsedPrice, if an item doesn't have this tag then the error is raised.
I am new to Python and coding so struggling along as best I can...

3

There are 3 answers

6
MAP On BEST ANSWER

I believe you have a bad indentation problem. Python defines a block by its indentation. Your if/else structure is outside the for loop. This is what you might be looking for:

price_list = [{}]
for i in price_search:
  lnp = i.LowestNewPrice.FormattedPrice.text
  qty_n = i.TotalNew.text
  qty_u = i.TotalUsed.text
  int_qty_u = int(qty_u)
  if int_qty_u > 0:
      lup = i.LowestUsedPrice.FormattedPrice.text
  else:
      continue
  price_list.append({'Lowest New Price': lnp, 'Lowest Used Price': lup, 'Quantity New': qty_n, 'Quantity Used': qty_u})

Other than that, use a try-except clause to handle exceptional values or cases and return the program to a valid state. An example could be:

 if int_qty_u > 0:
      try:
          lup = i.LowestUsedPrice.FormattedPrice.text
      except: #we catch any exception that could happend
          lup = '<null>' #just to put a string 

for sake of completition i would do a try-except on all the for's block:

price_list = [{}]
for i in price_search:
  try:
      lnp = i.LowestNewPrice.FormattedPrice.text
      qty_n = i.TotalNew.text
      qty_u = i.TotalUsed.text
      int_qty_u = int(qty_u)
      if int_qty_u > 0:
          lup = i.LowestUsedPrice.FormattedPrice.text
      else:
         continue
  except:
      lnp,qty_n,qty_u,int_qty_u='null','null','null',-1 #multiple assignment in a bad case
  price_list.append({'Lowest New Price': lnp, 'Lowest Used Price': lup, 'Quantity New': qty_n, 'Quantity Used': qty_u})
0
Barmar On

continue goes to the next iteration of the loop, so you skip the rest of the loop body. Instead of that, you should assign a default value to the variable.

if int_qty_u > 0:
    lup = i.LowestUsedPrice.FormattedPrice.text
else:
    lup = "some default value"
0
Quinn On

You could also try adding a check if the item has the tag LowestUsedPrice:

price_list = [{}]

for i in price_search:
    lnp = i.LowestNewPrice.FormattedPrice.text
    qty_n = i.TotalNew.text
    qty_u = i.TotalUsed.text
    int_qty_u = int(qty_u)
if int_qty_u > 0 and i.LowestUsedPrice != None:
    lup = i.LowestUsedPrice.FormattedPrice.text
else:
    lup = 'null'

price_list.append({'Lowest New Price': lnp, 'Lowest Used Price': lup, 'Quantity New': qty_n, 'Quantity Used': qty_u})