英文:
Storing data in files even if the program is closed in Python
问题
以下是您的代码部分的翻译:
library_list = ["Lord of the Flies", "Cat in the Hat", "Green Eggs and Ham", "One Fish Two Fish"]
user_list = []
checkout = False
f = open("savedata.txt", "w")
#username = school
#password = password
user_check = input("What is your username \n")
password_check = input("\n" + "What is your password?")
while checkout == False:
option = input("Welcome to the library. What book would you like to borrow?" + str(library_list))
if option in library_list:
library_list.remove(option)
user_list.append(option)
LIBRARY_LIST = library_list
USER_LIST = user_list
print("Library catalogue:" + str(library_list) + "\n" + "Borrowed books:" + str(user_list))
f.write(str(LIBRARY_LIST) + "\n" + str(USER_LIST))
orderConfirm = input("Would you like to borrow another book? (y/n)")
# Pathways converge after user confirms order.
if orderConfirm.lower() == "n":
print("Thanks for coming!")
break
else:
print("Okay, here is our catalogue.")
elif option in user_list:
print("I'm sorry, this item is currently borrowed by someone else. Please try again later.")
else:
print("This item is not in the library. Try again.")
这是您尝试使用的另一段代码的翻译:
with open("savelist.txt", "r") as f:
for line in f:
print(line)
请注意,您的原始代码中包含HTML转义字符(例如"
),我已经将其还原为普通的引号以使代码更易阅读。
英文:
I'm working for a school project and I can't seem to find the answers for this. The main trouble with my assignment is to:
> Make sure to use Python's file handling capabilities to ensure that data is persistent even after the program stops running.
How can I make it so that I can update my lists through the data stored in my file? By this I mean that I would edit my list in my while loop, save it to the file, and then change the list based on what is written in the file. I also cannot use tkinter for this project. This is my current code:
library_list = ["Lord of the Flies", "Cat in the Hat", "Green Eggs and Ham", "One Fish Two Fish"]
user_list = []
checkout = False
f = open("savedata.txt", "w")
#username = school
#password = password
user_check = input("What is your username \n")
password_check = input("\n" + "What is your password?")
while checkout == False:
option = input("Welcome to the library. What book would you like to borrow?" + str(library_list))
if option in library_list:
library_list.remove(option)
user_list.append(option)
LIBRARY_LIST = library_list
USER_LIST = user_list
print("Library catalogue:" + str(library_list) + "\n" + "Borrowed books:" + str(user_list))
f.write(str(LIBRARY_LIST) + "\n" + str(USER_LIST))
orderConfirm = input("Would you like to borrow another book? (y/n)")
# Pathways converge after user confirms order.
if orderConfirm.lower() == "n":
print("Thanks for coming!")
break
else:
print("Okay, here is our catalogue.")
elif option in user_list:
print("I'm sorry, this item is currently borrowed by someone else. Please try again later.")
else:
print("This item is not in the library. Try again.")`
Thank you.
I tried to use the following code but it just skips over it.
with open("savelist.txt", "r") as f:
for line in f:
print(line)
答案1
得分: 1
首先创建你的初始数据:
import json
INITIAL_LIST = []
# 如果保存文件尚未创建
if not os.path.exists("/path/to/savefile.dat"):
with open("/path/to/savefile.dat","wb") as f:
# 保存初始列表
json.dump(INITIAL_LIST,f)
然后打开并读取你的 "saved data":
library_list = json.load(open("/path/to/savefile.dat","rb"))
接着修改你的列表:
# 添加一本书
library_list.append(new_book_data)
然后定期保存它:
with open("/path/to/savefile.dat","wb") as f:
json.dump(library_list,f)
这是完成这个任务的一种潜在方式。
英文:
make your initial data first
import json
INITIAL_LIST = []
# if the savefile is not yet created
if not os.path.exists("/path/to/savefile.dat"):
with open("/path/to/savefile.dat","wb") as f:
# save initial list
json.dump(INITIAL_LIST,f)
then open and read in your "saved data"
library_list = json.load(open("/path/to/savefile.dat","rb"))
then modify your list
# add a book
library_list.append(new_book_data)
then save it at regular intervals
with open("/path/to/savefile.dat","wb") as f:
json.dump(library_list,f)
this is one potential way to accomplish this task
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论