为什么我在查看文件时出现值错误?
Why am I getting value error in viewing a file?
我正在尝试在 Python 中创建密码管理器,但出现值错误。下面是我为密码管理器编写的代码。它使用密码输入。
def view():
with open("passwords.txt", "r") as f:
for line in f.readline():
data = (line.rstrip())
user, passw = data.split("|")
print("User: ", user, "password", passw)
def add():
name = input("Account Name: ")
pwd = input("Password:" )
with open("passwords.txt", "a") as f:
f.write(name + "|" + pwd + "\n")
while True:
mode = input ("Would you like to add a new password or view existing ones (view, add). Press q to quit").lower()
if mode == "q":
quit()
if mode == "view":
view()
elif mode == "add":
add()
else:
print("You have entered an invalid mode")
continue
这是我在 运行 和输入输入时遇到的错误:
File "C:\Users\user\Documents\Python\exercises\passwordmanag.py", line 22, in <module>
view()
File "C:\Users\user\Documents\Python\exercises\passwordmanag.py", line 8, in view
user, passw = data.split("|")
ValueError: not enough values to unpack (expected 2, got 1)
有人可以帮我解决这个错误吗?
谢谢。
def view():
with open("passwords.txt", "r") as f:
for line in f:
data = (line.rstrip())
user, passw = data.split("|")
print("User: ", user, "password", passw)
def add():
name = input("Account Name: ")
pwd = input("Password:" )
with open("passwords.txt", "a") as f:
f.write(name + "|" + pwd + "\n")
while True:
mode = input ("Would you like to add a new password or view existing ones (view, add). Press q to quit").lower()
if mode == "q":
quit()
if mode == "view":
view()
elif mode == "add":
add()
else:
print("You have entered an invalid mode")
continue
上下文:您试图读取行中的每个字符而不是读取每一行,因此在尝试拆分时给您一个错误。
我正在尝试在 Python 中创建密码管理器,但出现值错误。下面是我为密码管理器编写的代码。它使用密码输入。
def view():
with open("passwords.txt", "r") as f:
for line in f.readline():
data = (line.rstrip())
user, passw = data.split("|")
print("User: ", user, "password", passw)
def add():
name = input("Account Name: ")
pwd = input("Password:" )
with open("passwords.txt", "a") as f:
f.write(name + "|" + pwd + "\n")
while True:
mode = input ("Would you like to add a new password or view existing ones (view, add). Press q to quit").lower()
if mode == "q":
quit()
if mode == "view":
view()
elif mode == "add":
add()
else:
print("You have entered an invalid mode")
continue
这是我在 运行 和输入输入时遇到的错误:
File "C:\Users\user\Documents\Python\exercises\passwordmanag.py", line 22, in <module>
view()
File "C:\Users\user\Documents\Python\exercises\passwordmanag.py", line 8, in view
user, passw = data.split("|")
ValueError: not enough values to unpack (expected 2, got 1)
有人可以帮我解决这个错误吗?
谢谢。
def view():
with open("passwords.txt", "r") as f:
for line in f:
data = (line.rstrip())
user, passw = data.split("|")
print("User: ", user, "password", passw)
def add():
name = input("Account Name: ")
pwd = input("Password:" )
with open("passwords.txt", "a") as f:
f.write(name + "|" + pwd + "\n")
while True:
mode = input ("Would you like to add a new password or view existing ones (view, add). Press q to quit").lower()
if mode == "q":
quit()
if mode == "view":
view()
elif mode == "add":
add()
else:
print("You have entered an invalid mode")
continue
上下文:您试图读取行中的每个字符而不是读取每一行,因此在尝试拆分时给您一个错误。