Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 populating csv and searching the file
#1
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:
Output:
Traceback (most recent call last): File "pwkpr1.py", line 80, in <module> prime() File "pwkpr1.py", line 16, in prime get_pw(repository) File "pwkpr1.py", line 55, in get_pw locate_site_row = sn_list.index(site_name) ValueError: 'fb' is not in list
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).
Quote
#2
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)  
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Populating a timetable with subjects brittocj 1 186 May-02-2019, 07:00 AM
Last Post: buran
  Linear search/searching for a word in a file/list kietrichards 3 506 Mar-08-2019, 07:58 PM
Last Post: Larz60+
  searching file for unique words Siylo 2 520 Nov-20-2018, 08:28 PM
Last Post: wavic
  Populating Array2 from Array1 PappaBear 1 454 Aug-22-2018, 04:30 AM
Last Post: PappaBear
  Populating a list with divisors RedSkeleton007 1 488 Aug-21-2018, 12:52 AM
Last Post: Larz60+
  for loop and list populating mepyyeti 3 1,002 Apr-12-2018, 03:06 AM
Last Post: buran
  Searching a text file to find words matching a pattern Micael 3 19,193 Nov-07-2017, 08:52 PM
Last Post: Micael
  Importing file in dataframe and populating missing column name vvv 1 1,323 Feb-26-2017, 02:32 PM
Last Post: sparkz_alot

Forum Jump:


Users browsing this thread: 1 Guest(s)