![]() |
populating csv and searching the file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: populating csv and searching the file (/thread-9743.html) |
populating csv and searching the file - mepyyeti - Apr-26-2018 So this has been a long term project I can't seem to crack. I've decided to post the problem start from the initial 'bad' function, then the traceback. Finally the whole code. I hope isolating the issue in this way makes it easier to look at the code...even if the post is longer (apologies if it has the opposite effect...unintended). THank you in advance...I can't figure this out myself... This is the code snippet/function containing the initial snag. Please see comments in lines 23-25... def get_pw(foo): #ask for site address site_name = input('Please enter site name...') print('Did you include an tld ext in the site name? .com? .edu? etc?') ext_remover = input('enter \'yes\' or \'no\'') #since most ppl won't follow directions and will include tld exts if ext_remover.strip().lower() == 'yes' or ext_remover == 'y': remove_tld(site_name) else: print('good job...;) ') print('\n\nfoo\n\n') filen = 'pwkpr.csv' sn_list = [] pw_list = [] with open(filen, 'a+',newline='') as f: file_read = csv.reader(f) for line in file_read: sname = line[0]#get site_names pword = line[1]#get pws sn_list.append(sname) pw_list.append(pword) #bc my csv is empty locate_site_row should be None... locate_site_row = sn_list.index(site_name) #line below should be triggered if locate_site_row ==None: add_entry(site_name) #if record exists, just look it up else: locate_pw = pw_list[locate_site_row] print(f'password for {site_name} is {locate_pw}')the error I get: the code in its entirety... initial problem is line 56/57. variable locate_line_row should be None...but it still doesn't work.#!usr/bin/env python3 #pwkpr1.py import os, csv, sys def prime(): using = 0#simple counter while True: os.chdir('/home/me/Desktop') print(os.getcwd()) repository=input('enter \'start\' to access repository') #check if user want to proceed if repository.strip().lower() == 'start' or repository == 's': using += 1 #user elects to proceed get_pw(repository) repository = input('\'start\' to go again...anything else to leave...') if repository.strip().lower() == 'start' or repository == 's': continue else: #user enters something other than 'start'/'s' #ask to confirm that they want to quit repository =input('are you sure you want to quit?\n\'yes\' to exit, \'no\' to start') #user changes their mind. Wants to looks up password afterall if repository.strip().lower() == 'no' or repository =='n': get_pw(repository) #user wants to quit. else: print(f'you used system {using} times(s)') sys.sleep(2) sys.exit() def get_pw(foo): #ask for site address site_name = input('Please enter site name...') print('Did you include an tld ext in the site name? .com? .edu? etc?') ext_remover = input('enter \'yes\' or \'no\'') #since most ppl won't follow directions and will include tld exts if ext_remover.strip().lower() == 'yes' or ext_remover == 'y': remove_tld(site_name) else: print('good job...;) ') print('\n\nfoo\n\n') filen = 'pwkpr.csv' sn_list = [] pw_list = [] with open(filen, 'a+',newline='') as f: file_read = csv.reader(f) for line in file_read: sname = line[0]#get site_names pword = line[1]#get pws sn_list.append(sname) pw_list.append(pword) #bc my csv is empty locate_site_row should be None... locate_site_row = sn_list.index(site_name) #line below should be triggered if locate_site_row ==None: add_entry(site_name) #if record exists, just look it up else: locate_pw = pw_list[locate_site_row] print(f'password for {site_name} is {locate_pw}') #just practice with splitext()...works def remove_tld(site): name, tld = os.path.splitext(site.strip().lower()) print(f'site is {name}.') return name #func to create an entry if none exists. no parameter needed? def add_entry(site): pw = input('Please enter password.') pw = pw.strip() with open(filen, 'a+') as f: #delimiter should be necessary to add comma add_line = csv.writer(f, delimiter=',') #site_name value from get_pw() add_line.writerow(site_name, pw) prime()I hope isolating the issue in this way (snippet, traceback, entire code) makes it easier to look at the code...even if the post is longer (apologies if it has the opposite effect...unintended). RE: populating csv and searching the file - woooee - Apr-26-2018 Check first locate_site_row=None if site_name in sn_list: locate_site_row = sn_list.index(site_name) else: add_entry(site_name) |