Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!rt.uk.eu.org!nntp-feed.chiark.greenend.org.uk!ewrotcd!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '%s"': 0.07; 'false.': 0.07; 'raised': 0.07; 'try:': 0.07; 'valueerror:': 0.07; 'python': 0.09; 'dvi': 0.09; 'typeerror:': 0.09; 'def': 0.10; 'index': 0.13; '(empty': 0.16; 'anymore': 0.16; 'csv': 0.16; 'enough.': 0.16; 'error"': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'incremented': 0.16; 'indexerror:': 0.16; 'iterating': 0.16; 'lcd': 0.16; 'match:': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'row': 0.16; 'subject:compare': 0.16; 'true:': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'changes': 0.20; 'equivalent': 0.20; 'sets': 0.23; 'script': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'values': 0.26; '(see': 0.27; 'possibly': 0.27; 'finds': 0.29; 'received:192.168.1.3': 0.29; "i'm": 0.29; 'normally': 0.30; 'speakers': 0.30; 'error': 0.30; '(and': 0.32; 'problem.': 0.32; 'could': 0.32; 'print': 0.32; 'anywhere': 0.33; 'says': 0.33; 'to:addr:python-list': 0.33; 'code:': 0.33; 'guys': 0.33; 'changed': 0.34; 'updated': 0.34; 'false': 0.35; 'doing': 0.35; 'except': 0.36; 'skip:u 20': 0.36; 'but': 0.36; 'stock': 0.36; "didn't": 0.36; 'possible': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'gives': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'most': 0.61; 'places': 0.61; 'wide': 0.62; 'times': 0.63; 'within': 0.64; 'here': 0.65; 'total': 0.65; 'skip:m 50': 0.65; 'price': 0.66; 'header:Reply- To:1': 0.68; 'products': 0.70; 'reply-to:no real name:2**0': 0.72; 'confusing': 0.84; 'reply-to:addr:python.org': 0.84; 'reports:': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=dNckaZlb c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=O2Kvzccb_dQA:10 a=jlvM8aAObukA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=1pmcVapBfBAA:10 a=ir0_l5OPIRIKwo6RENQA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Thu, 06 Dec 2012 17:10:00 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Confused compare function :) References: <50c01fe2$0$21853$c3e8da3$76491128@news.astraweb.com> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 92 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354813801 news.xs4all.nl 6953 [2001:888:2000:d::a6]:33238 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34407 On 2012-12-06 14:22, Anatoli Hristov wrote: > Guys I'm still confusing my script is working better, but not enough. > I did a logfile to see which products are not found anymore in the CSV > and I found some that are present but python says they are not ?? > > Here is the product in the CSV: > MONIIE2407HDS-B1;MON;II;E2407HDS-B1;E2407HDS-B1;IIYAMA LCD 24" Wide > 1920x1080TN Speakers 2ms Black DVI HDMI;133;20;RECTD0.41;0,41;;;;;;;;; > > Here is what python reports: > e2208hds-b2, 721 not found > e2273hds-b1, 722 not found > e2274hds-b2, 723 not found > e2407hds-b1, 724 not found > > And here is my final code: ( I hope it look better now :) ) > > def Change_price(): # Changes the price in the DB if the price in the CSV is changed > TotalUpdated = 0 # Counter for total updated > TotalSKUFound = 0 # Total SKU from the DB coresponds to the one in the CSV > TotalSKUinDB = 0 # Total SKU in the DB > for row in PRODUCTSDB: > TotalSKUinDB +=1 > db_sku = row["sku"].lower() > db_price = float(row["price"]) > found = False > try: > for x in pricelist: > try: > 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 > match = re.search(db_sku, csv_sku) This line is equivalent to: match = db_sku in csv_sku This means that it's looking for db_sku anywhere in csv_sku. For example, all of these are true: "72" in "720"; "20" in "720"; "720" in "720"; etc. > if len(db_sku) != 0 and match: > TotalSKUFound +=1 TotalSKUFound is incremented if it finds a match. Can "db_sku in csv_sku" be true multiple times for a given value of db_sku? (See above.) Is it possible that TotalSKUFound is incremented multiple times for some values of csv_sku? > if csv_new_price < db_price and csv_stock > 0: > print db_sku, csv_price, db_price, csv_new_price > Update_SQL(csv_new_price, db_sku) > TotalUpdated += 1 > found = True It sets found to True if it updated. > > except IndexError: # I have a lot of index error in the CSV (empty fields) and the loop gives "index error" I don't care about them > pass > except ValueError: > pass > except TypeError: > pass Even after finding a match (and possibly updating), it continues iterating though pricelist. > except IndexError: There's no need to catch IndexError here because the only places it could be raised are also within the inner try..except. > pass > if not found: WriteLog(db_sku, db_sku,) 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. > TotalNotFound = TotalSKUinDB - TotalSKUFound > print "Total SKU in the DB %s" % TotalSKUinDB > print "Total SKU coresponds to the DB and CSV %s" % TotalSKUFound > print "Total updated: %s" % TotalUpdated > print"Total not found with in the distributor: %s" % TotalNotFound > 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.