Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'ignored': 0.05; 'modify': 0.05; '%s"': 0.07; '__name__': 0.07; 'function,': 0.07; 'parsing': 0.07; "'''": 0.09; 'rows': 0.09; 'script,': 0.09; 'subject:files': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; "'__main__':": 0.16; 'belongs': 0.16; 'cleaner': 0.16; 'commented': 0.16; 'csv': 0.16; 'row': 0.16; 'similarly,': 0.16; 'skip:n 50': 0.16; 'writer': 0.16; 'wrote:': 0.17; 'creates': 0.18; 'input': 0.18; 'code.': 0.20; 'bit': 0.21; 'import': 0.21; 'constant': 0.22; 'insert': 0.23; 'nearly': 0.23; 'specified': 0.23; 'cc:no real name:2**0': 0.24; 'cc:2**1': 0.24; 'script': 0.24; 'pass': 0.25; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'subject:list': 0.28; 'once.': 0.29; "i'm": 0.29; 'writes': 0.30; 'figure': 0.30; 'stuff': 0.30; '(and': 0.32; 'problem.': 0.32; 'file': 0.32; 'addresses': 0.32; 'print': 0.32; 'says': 0.33; 'problem': 0.33; "can't": 0.34; 'list': 0.35; 'lists.': 0.35; 'returning': 0.35; 'open': 0.35; 'but': 0.36; 'wanted': 0.36; 'compare': 0.36; 'should': 0.36; 'too': 0.36; 'skip:p 20': 0.36; 'why': 0.37; 'quite': 0.37; 'well.': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'received:192': 0.39; 'hello,': 0.39; 'called': 0.39; 'little': 0.39; 'skip:" 10': 0.40; 'received:192.168': 0.40; 'end': 0.40; 'think': 0.40; 'your': 0.60; 'range': 0.60; "you've": 0.61; 'containing': 0.61; 'first': 0.61; 'strange': 0.62; 'different': 0.63; 'times': 0.63; 'skip:n 10': 0.63; 'more': 0.63; 'here': 0.65; 'therefore': 0.65; 'treat': 0.65; 'header:Reply-To:1': 0.68; 'phone': 0.68; 'received:74.208': 0.71; 'reply-to:no real name:2**0': 0.72; 'tel': 0.75; "it'd": 0.84; 'modify.': 0.84; 'phones:': 0.84; 'serious': 0.98 Date: Thu, 29 Nov 2012 22:17:09 -0500 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 MIME-Version: 1.0 To: Anatoli Hristov Subject: Re: Compare list entry from csv files References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:8W8Tf8h6QSFW+GAcxGRgQkO7vaZBOHtrhxiovV2PNqk IsigBx47bY7FyBgYa0rUXBWpIiU8zmdSqe6XWT5G7Uu4cp5h3q bTz11TjJTBjxzjesB3+nTxeW4WpkZg4x3Kk66unPvIk2lNwZ5c IV9cq/kV34QboVc4YJG96xSI+p9C6LrA+6td0Z4Ci3eqMWt1je AdgdELswe5hHAYkirLH7ELoQEXUCOJ/aWtmqRucPVEPOiQu3zM YUGcrPq5IJ+ywLk3APmzfu5Z1jHI3XpJyhrGaBR0rKyLqoE4m8 hEvQ0+8DF0cdISb6RCq7wJ4IhaFUCYz/2PNKfiXWT7YVb6gKg= = Cc: python-list@python.org, Neil Cerutti X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: d@davea.name 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: 103 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354245470 news.xs4all.nl 6871 [2001:888:2000:d::a6]:57904 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34086 On 11/29/2012 05:22 AM, Anatoli Hristov wrote: > > Hello, > > Tried to document a little bit the script, but I'm not that good in that too :) > > The only problem I have is that I cant compare other field than the > first one in > for ex_phone in phones: > telstr = ex_phone[0].lower() > When I use telstr = ex_phone[0].lower() it says out of range and the > strange think is that the range is 6 I can't figure that out. So when > I edit the csv I modify the look of the file and then I start the > script and it works, but I wanted to use more than one condition and I > can't :( > > > > > import csv > > # Open the file with the names and addresses > origf = open('c:/Working/vpharma.csv', 'rt') > # Open the file with the phone numbers > secfile = open('c:/Working/navori.csv', 'rt') > > # Creates the empty list with the names > namelist = [] > # Creates the empty list with the phone numbers > PHONELIST = [] > > > # Reads the file with the names > # Format "Name","Phone" > names = csv.reader(origf, delimiter=';') > > # Reads the file with the phone numbers > # Format "First name","Lastname","Address","City","Country","Phone" > phones = csv.reader(secfile, delimiter=';') > > # Creates a list with phone numbers > #for tel in phones: > # PHONELIST.append(tel) Without populating the PHONELIST here, you have a serious problem. Why is it commented out? > > def finder(Compare_Name,rows): > ''' > Compare the names from the namelist with the names from the phonelist. > If the name match - then the phone number is added to the specified field > ''' > for ex_phone in phones: You should be using PHONELIST here as well. phones is a pseudo-file, which can only be traversed once. A list can be traversed as many times as you like, which is quite a few in your code. > telstr = ex_phone[0].lower() > print telstr > if telstr.find(Compare_Name) >= 0: > print "\nName found: %s" % Compare_Name > namelist[rows][-1] = ex_phone[-1].lower() > else: > print "Not found %s" % Compare_Name > pass > return > > def name_find(): > rows = 0 > for row in names: > namelist.append(row) > Compare_Name = row[1].lower() > finder(Compare_Name,rows) > rows = rows+1 > > if __name__ == '__main__': > name_find() > > # Writes the list to a file > wfile = open('c:/Working/ttest.csv', "wb") > writer = csv.writer(wfile, delimiter=';') > for insert in namelist: > writer.writerow(insert) > wfile.close() As I said before, process both files into lists, one that you treat as constant (and therefore capitalized) and the other containing the data you intend to modify. It'd be much cleaner if you did all that input file parsing stuff in one function, returning only the lists. Call it just before calling name_find(). Similarly, the part you have at the end belongs in a different function, called just after calling name_find(). There's lots of other stuff that should be cleaner, but you've ignored nearly all the suggestions from various people. -- DaveA