Python Forum
populating csv and searching the file
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).
Reply
#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)  
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  splitting file into multiple files by searching for string AlphaInc 2 816 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Populating an array dynamically zxcv101 1 1,106 May-17-2022, 11:24 AM
Last Post: deanhystad
  help with project of reading and searching big log file korenron 6 2,123 Jun-24-2021, 01:57 PM
Last Post: korenron
  Searching string in file and save next line dani8586 2 2,250 Jul-10-2020, 09:03 AM
Last Post: dani8586
  Populating a timetable with subjects brittocj 1 1,777 May-02-2019, 07:00 AM
Last Post: buran
  Linear search/searching for a word in a file/list kietrichards 3 3,382 Mar-08-2019, 07:58 PM
Last Post: Larz60+
  searching file for unique words Siylo 2 2,476 Nov-20-2018, 08:28 PM
Last Post: wavic
  Populating Array2 from Array1 PappaBear 1 2,034 Aug-22-2018, 04:30 AM
Last Post: PappaBear
  Populating a list with divisors RedSkeleton007 1 2,148 Aug-21-2018, 12:52 AM
Last Post: Larz60+
  for loop and list populating mepyyeti 3 3,406 Apr-12-2018, 03:06 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020