【发布时间】:2020-12-06 07:37:42
【问题描述】:
这是我正在处理的代码...
这里 Name.dat 是空文件...
# Library Management System
import time
import pickle
import csv
print("Welcome to the Library Management System")
time.sleep(0.3)
print("Hello there",end="")
print(".",end="")
time.sleep(0.5)
print(".",end="")
time.sleep(0.5)
print(".")
time.sleep(0.5)
while True:
try:
print("If you are new user press 1 to continue::")
print("If you are an existing user press 2 to continue::")
print("If you want to exit press 3::")
time.sleep(0.5)
n=int(input("Enter your choice::"))
except:
print("Only integer values.")
try:
if n==1:
print("here")
name_list=[]
print("here")
f=open("Name.dat","rb+")
print("here")
s=pickle.load(f)
print("here")
if len(s)==0:
pass
else:
for i in s:
name_list.append(i)
f=open("Name.dat","ab+")
l=[]
user=input("Enter username::")
while True:
truth=1
if len(name_list)==0:
pass
else:
for i in range(len(name_list)):
if user==name_list[i][0]:
truth=0
if truth==1:
break
user=input("Enter username::")
l.append(user)
ps=input("Enter password::")
l.append(ps)
l.append([])
pickle.dump(l,f)
f.close()
print("Your account has been successfully made.")
elif n==2:
f=open("Name.dat","rb+")
b=int(input("Enter the password::"))
try:
while True:
s=pickle.load(f)
if s[1]==b:
print("Hello",s[0])
print("What do you want to do?")
print("Enter 1 to borrow a book::")
print("Enter 2 to return a book::")
except EOFError:
f.close()
elif n==3:
f=open("csv_file.csv",'r')
csvr=csv.reader(f)
for line in csvr:
#copying data into a temporary storage area from csv file
print(line)
f.close()
break
elif n>3:
print("Wrong input")
except IOError:
print("swomething")
None
当我在python shell中输入1时出现问题...
Welcome to the Library Management System
Hello there...
If you are new user press 1 to continue::
If you are an existing user press 2 to continue::
If you want to exit press 3::
Enter your choice::1
here
here
here
Traceback (most recent call last):
File "C:\Users\CCFFIN\AppData\Local\Programs\Python\Python38\Python big big c project\LBS.py", line 30, in <module>
s=pickle.load(f)
EOFError: Ran out of input
错误出现在 pickle .load 中,但我不知道为什么会这样
是因为 Name.dat 是空的吗?
我基本上想要的是这样的东西,没有任何例外
Welcome to the Library Management System
Hello there...
If you are new user press 1 to continue::
If you are an existing user press 2 to continue::
If you want to exit press 3::
Enter your choice::1
here
here
here
here
Enter username::
请注意,我在这里打印是为了识别错误,因此没有必要
csv_file 不为空,里面有 51 个列表
还请说明为什么转储不起作用以及为什么会出现 EofError
提前谢谢你
【问题讨论】:
-
欢迎来到 Stack Overflow。请阅读how to ask a good question,因为您的问题存在许多问题。就个人而言,我认为它很长(尽管并不总是很糟糕)。但是你一次问多个问题,这是不允许的。而且您的代码比必要的要长得多。请专注于一个问题并为它创建一个minimal reproducible example。如果您的代码导致错误,请包含完整的错误跟踪。那时你可能会得到更好的回应。祝你好运!
-
关于你的问题的内容:想要达到什么目的?为什么使用
pickle?这对我来说似乎很奇怪,但我不知道你的意图。无论如何,如果你真的应该使用pickle来读取文件,那么文件不应该是空的。 -
好的先生,我会从下一个问题中解决这个问题
-
谢谢,但实际上你也应该尝试改进这个问题:-)