Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'binary': 0.05; 'modify': 0.05; '%s"': 0.07; '__name__': 0.07; 'python': 0.09; "'''": 0.09; '3.0,': 0.09; '3.0.': 0.09; 'argument,': 0.09; 'rows': 0.09; 'rows,': 0.09; 'script,': 0.09; 'similar,': 0.09; 'subject:files': 0.09; 'undocumented': 0.09; 'cc:addr:python- list': 0.10; 'def': 0.10; "'__main__':": 0.16; 'csv': 0.16; 'expects': 0.16; 'integers.': 0.16; 'perfect.': 0.16; 'row': 0.16; 'skip:n 50': 0.16; 'writer': 0.16; 'wrote:': 0.17; 'creates': 0.18; 'module': 0.19; 'versions': 0.20; 'bit': 0.21; 'import': 0.21; 'received:209.85.214.174': 0.21; 'names.': 0.22; 'cc:2**0': 0.23; 'insert': 0.23; 'specified': 0.23; 'cc:no real name:2**0': 0.24; 'header': 0.24; 'script': 0.24; 'pass': 0.25; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'instead.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'subject:list': 0.28; 'url:mailman': 0.29; "i'm": 0.29; 'keyword': 0.30; 'mode': 0.30; 'notes': 0.30; 'writes': 0.30; 'function': 0.30; 'figure': 0.30; 'code': 0.31; 'you?': 0.32; 'url:python': 0.32; 'file': 0.32; 'addresses': 0.32; 'help,': 0.32; 'structure': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'says': 0.33; 'problem': 0.33; 'code:': 0.33; 'version': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'list': 0.35; 'nov': 0.35; 'open': 0.35; 'pm,': 0.35; 'sometimes': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'but': 0.36; 'wanted': 0.36; 'url:org': 0.36; 'compare': 0.36; 'useful': 0.36; 'should': 0.36; 'thank': 0.36; 'too': 0.36; 'skip:p 20': 0.36; 'being': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'instead': 0.39; 'received:209.85.214': 0.39; 'hello,': 0.39; 'short': 0.39; 'little': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'range': 0.60; 'first': 0.61; 'strange': 0.62; 'skip:n 10': 0.63; 'more': 0.63; 'here': 0.65; 'phone': 0.68; 'records': 0.68; 'smith': 0.71; 'special': 0.73; 'tel': 0.75; 'cuts': 0.84; 'donald': 0.84; 'neil': 0.84; 'phones:': 0.84; 'habit': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=5fzyPBdsf0zKmtDZgh0sKYHzyIXwxlYBR2Plnv1EkpY=; b=gNW/ihBWJouyRTrdgePFCq6kZwf9Zeh+YSAOPa3+z/SPujSEzfZyghCuLktsJQ7Fb7 DELt/QegzmebI9ZOFOQ7FJR73B29kYjWhe0JV/EaKHvRX8rQ7Qel64NWE/sa1OPAfkte 9PAT9ogIv2bNbJtvXaYAHRJFtNwfRdBozrIVDgTcWhLjyIZB90wsH0X436rUnvYYzSGn WGDM84aNst4NxonDXy+gioJTSjvUS1BRKGpBMsvObC9OJLVbtUiUCkUQaaPFpaYx+lA6 I5Lo3J0Fjej77kszQzEm6QyawGy20x2OT80y/5JMbO7JDD9t3yKjAtsszYKs9ZXs4oDR 6YCg== MIME-Version: 1.0 In-Reply-To: References: <50B3E865.9070700@davea.name> <50B43246.2010902@davea.name> Date: Thu, 29 Nov 2012 11:22:28 +0100 Subject: Re: Compare list entry from csv files From: Anatoli Hristov To: Neil Cerutti Content-Type: text/plain; charset=UTF-8 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list 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: 166 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354184551 news.xs4all.nl 6855 [2001:888:2000:d::a6]:53626 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34059 On Tue, Nov 27, 2012 at 9:41 PM, Neil Cerutti wrote: > On 2012-11-27, Anatoli Hristov wrote: >> Thank you all for the help, but I figured that out and the >> program now works perfect. I would appreciate if you have some >> notes about my script as I'm noob :) Here is the code: >> >> import csv >> >> origf = open('c:/Working/Test_phonebook.csv', 'rt') >> secfile = open('c:/Working/phones.csv', 'rt') > > csv module expects files to be opened in binary mode in Python > versions less than version 3.0. For Python versions >= 3.0, you > use the special keyword argument, newlines='', instead. > >> phonelist = [] >> namelist = [] > > The structure of your program is poor. It's workable for such a > short script, and sometimes my first cuts are similar, but it's > better to get out of the habit right away. > > Once you get this working the way you'd like you should clean up > the structure as a service to your future self. > >> names = csv.reader(origf, delimiter=';') >> phones = csv.reader(secfile, delimiter=';') > > You csv files don't seem to have header rows, but even so you can > improve your code by providing fieldnames and using a DictReader > instead. > > name_reader = csv.DictReader(origf, fieldnames=[ > 'Name', 'Blah', 'Phone#']) > > Then you can read from records with > > name = row['Name'] > > instead of using bare, undocumented integers. > >> for tel in phones: >> phonelist.append(tel) >> >> def finder(name_row,rows): >> for ex_phone in phonelist: >> telstr = ex_phone[0].lower() >> if telstr.find(name_row) >= 0: > > This strikes me as a crude way to match names. You don't really > want Donald to match perfectly with McDonald, do you? Or for > Smith to match with Smithfield? > > Yes, a human being will clean it up, but your program can do a > better job. > >> print "\nName found: %s" % name_row >> namelist[rows][-1] = ex_phone[-1].lower() >> else: >> pass >> return >> >> def name_find(): >> rows = 0 >> for row in names: >> namelist.append(row) >> name_row = row[0].lower() >> finder(name_row,rows) >> rows = rows+1 > > You can use the useful enumerate function instead of your own > counter. > > for rows, row in enumerate(names): > > ...though I would find 'rownum' or 'num' or just 'i' better than > the name 'rows', which I find confusing. > >> name_find() >> ofile = open('c:/Working/ttest.csv', "wb") >> writer = csv.writer(wfile, delimiter=';') >> for insert in namelist: >> writer.writerow(insert) >> wfile.close() > > -- > Neil Cerutti > -- > http://mail.python.org/mailman/listinfo/python-list 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) 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: 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()