Confused compare function :)

Anatoli Hristov tolidtm at
Fri Dec 7 14:36:23 CET 2012

>> Calling it 'found' is misleading, because it's True only if it updated.
>> If it found a match but didn't update, 'found' will still be False.
>> Using a loop within a loop like this could be the cause of your
>> problem. It's certainly not the most efficient way of doing it.
> I will keep you posted THANK YOU

And here is my final code -- I hope you will like it a little more :)

def Change_price(): # Changes the price in the DB if the price in the
CSV is changed
    TotalUpdated = 0 # Counter for total updated
    TotalSKUNotFound = 0 # Total SKU from the DB coresponds to the one
in the CSV
    TotalSKUinDB = 0

    found = None
    for row in PRODUCTSDB:
        TotalSKUinDB +=1
        db_sku = row["sku"].lower()
        db_price = float(row["price"])
        if CompareWithCSV(db_sku, db_price) == True:
            TotalUpdated +=1
            TotalSKUNotFound +=1
            WriteLog(db_sku, row["product_id"])

    print "Total updated: %s" % TotalUpdated
    print"Total not found with in the distributor: %s" % TotalSKUNotFound
    print "Total products in the DB %s" % TotalSKUinDB

def CompareWithCSV(db_sku, db_price):
        for x in pricelist:
                csv_price = x[6]
                csv_price = csv_price.replace(",",".")
                csv_price = float(csv_price)
                csv_new_price = csv_price*1.10
                csv_sku = x[4].lower()
                csv_stock = int(x[7]) # I used this as normally I used
 stock in the condition
                if len(db_sku) != 0 and db_sku == csv_sku and csv_stock > 0:
                    print db_sku, csv_price, db_price, csv_new_price
                    Update_SQL(csv_new_price, db_sku)
                    return True
            except (IndexError, ValueError, TypeError, db_sku): # I
have a lot of index error in the CSV (empty fields) and the loop gives
"index error" I  don't care about them
                WriteErrorLog("Error with CSV file loop: ", db_sku)
    except IndexError:
        WriteErrorLog("Error with CSV file loop: ", db_sku)
    return False

More information about the Python-list mailing list