I'm currently creating a database API in order to get a better grasp of how APIs work. Currently fixing the errors and whatnot, when I tried the delete function (Option 3), it gives me an error and tells me that the split function is returning one value rather than two.
My guess is that it's either reading the lines incorrectly, or through reading the text file, it doesn't register the tab ('\t') but rather as a space (' ') at times.
I'm doing this through VS Code, not sure if that's a contributing factor at all with the indents and stuff. But I've got it at 4 spaces = tab like the Python IDLE does normally.
Here's the code:
import time
def add(filename):
print('Name?')
key = input()
print('Phone number?')
value = input()
f = open(filename, "a")
f.write(key + "\t" + value + "\n")
f.close()
def find(filename, key):
f = open(filename, "r")
for line in f:
#first stores the current key in variable
(currentKey, currentValue) = line.split('\t', 1)
if currentKey == key:
return currentValue[:-1]
f.close()
def delete(filename, key):
f = open(filename, "r")
f1 = open('temporary.txt', "w")
for (line) in f:
#Error occurs here
(currentKey, currentValue) = line.split('\t', 1)
if currentKey != key:
f1.write(line)
f.close()
f1.close()
import os
os.replace('temporary.txt', filename)
def update(filename, key, value):
resultfile = "temporary.txt"
f = open(filename, "r")
f1 = open(resultfile, "w")
for line in filename:
(currentKey, currentValue) = line.split('\t', 1)
if currentKey != key:
f1.write(line)
else:
f1.write(currentKey + '\t' + value + '\n')
f.close()
f1.close()
import os
os.replace(resultfile, filename)
filename = 'telephone.txt'
#Current Menu Interface
def menu():
print("Welcome to the telephone directory")
print("Your options are:")
print()
print("1) Adding")
print("2) Finding")
print("3) Deleting")
print("4) Updating")
print("5) Quitting")
choice = input()
return choice
#actual decision code
def decision(choice):
loop = 1
while loop == 1:
if choice == '1':
add(filename)
choice = menu()
elif choice == '2':
print("What is the name of the person you are looking for?")
key = input()
value = find(filename, key)
print(key + "'s phone number is: " + value)
choice = menu()
elif choice == '3':
print("What is the name of the person you are looking to delete from the directory?")
key = input()
delete(filename, key)
choice = menu()
elif choice == '4':
print("What is the name of the person?")
key = input()
print("What is the telephone number you want to replace it with?")
value = input()
update(filename, key, value)
choice = menu()
elif choice == '5':
print("Thank you for using the program! Your file contents are stored in a file called 'telephone.txt'.")
time.sleep(1)
loop = 0
else:
print("Please enter a value from 1 to 5. No spaces at all.")
choice = menu()
choice = menu()
decision(choice)
linebefore usingsplitline, the code is able to find the correctlineaccordingly. It prints out thekey(a name in this case), a significant\tin between and avalue(a number in this case).line.split(None,1)Nonevalue? In my file I've always set the'/t'value as the separator. But it seems like a space value in the txt file itself because of the way tabbing works.