在Python中,即使程序关闭,也可以将数据存储在文件中。

huangapple go评论95阅读模式
英文:

Storing data in files even if the program is closed in Python

问题

以下是您的代码部分的翻译:

  1. library_list = ["Lord of the Flies", "Cat in the Hat", "Green Eggs and Ham", "One Fish Two Fish"]
  2. user_list = []
  3. checkout = False
  4. f = open("savedata.txt", "w")
  5. #username = school
  6. #password = password
  7. user_check = input("What is your username \n")
  8. password_check = input("\n" + "What is your password?")
  9. while checkout == False:
  10. option = input("Welcome to the library. What book would you like to borrow?" + str(library_list))
  11. if option in library_list:
  12. library_list.remove(option)
  13. user_list.append(option)
  14. LIBRARY_LIST = library_list
  15. USER_LIST = user_list
  16. print("Library catalogue:" + str(library_list) + "\n" + "Borrowed books:" + str(user_list))
  17. f.write(str(LIBRARY_LIST) + "\n" + str(USER_LIST))
  18. orderConfirm = input("Would you like to borrow another book? (y/n)")
  19. # Pathways converge after user confirms order.
  20. if orderConfirm.lower() == "n":
  21. print("Thanks for coming!")
  22. break
  23. else:
  24. print("Okay, here is our catalogue.")
  25. elif option in user_list:
  26. print("I'm sorry, this item is currently borrowed by someone else. Please try again later.")
  27. else:
  28. print("This item is not in the library. Try again.")

这是您尝试使用的另一段代码的翻译:

  1. with open("savelist.txt", "r") as f:
  2. for line in f:
  3. 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:

  1. library_list = ["Lord of the Flies", "Cat in the Hat", "Green Eggs and Ham", "One Fish Two Fish"]
  2. user_list = []
  3. checkout = False
  4. f = open("savedata.txt", "w")
  5. #username = school
  6. #password = password
  7. user_check = input("What is your username \n")
  8. password_check = input("\n" + "What is your password?")
  9. while checkout == False:
  10. option = input("Welcome to the library. What book would you like to borrow?" + str(library_list))
  11. if option in library_list:
  12. library_list.remove(option)
  13. user_list.append(option)
  14. LIBRARY_LIST = library_list
  15. USER_LIST = user_list
  16. print("Library catalogue:" + str(library_list) + "\n" + "Borrowed books:" + str(user_list))
  17. f.write(str(LIBRARY_LIST) + "\n" + str(USER_LIST))
  18. orderConfirm = input("Would you like to borrow another book? (y/n)")
  19. # Pathways converge after user confirms order.
  20. if orderConfirm.lower() == "n":
  21. print("Thanks for coming!")
  22. break
  23. else:
  24. print("Okay, here is our catalogue.")
  25. elif option in user_list:
  26. print("I'm sorry, this item is currently borrowed by someone else. Please try again later.")
  27. else:
  28. 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.

  1. with open("savelist.txt", "r") as f:
  2. for line in f:
  3. print(line)

答案1

得分: 1

首先创建你的初始数据:

  1. import json
  2. INITIAL_LIST = []
  3. # 如果保存文件尚未创建
  4. if not os.path.exists("/path/to/savefile.dat"):
  5. with open("/path/to/savefile.dat","wb") as f:
  6. # 保存初始列表
  7. json.dump(INITIAL_LIST,f)

然后打开并读取你的 "saved data":

  1. library_list = json.load(open("/path/to/savefile.dat","rb"))

接着修改你的列表:

  1. # 添加一本书
  2. library_list.append(new_book_data)

然后定期保存它:

  1. with open("/path/to/savefile.dat","wb") as f:
  2. json.dump(library_list,f)

这是完成这个任务的一种潜在方式。

英文:

make your initial data first

  1. import json
  2. INITIAL_LIST = []
  3. # if the savefile is not yet created
  4. if not os.path.exists("/path/to/savefile.dat"):
  5. with open("/path/to/savefile.dat","wb") as f:
  6. # save initial list
  7. json.dump(INITIAL_LIST,f)

then open and read in your "saved data"

  1. library_list = json.load(open("/path/to/savefile.dat","rb"))

then modify your list

  1. # add a book
  2. library_list.append(new_book_data)

then save it at regular intervals

  1. with open("/path/to/savefile.dat","wb") as f:
  2. json.dump(library_list,f)

this is one potential way to accomplish this task

huangapple
  • 本文由 发表于 2023年6月6日 02:33:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定