Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
710 views
in Technique[技术] by (71.8m points)

saving data with lists and appending data to list as safety net, python

so what I've been trying to do is store data on a program so that way when I reopen my python program so all the previous data is still there. what I want to do is have the ability to use an input statement and allow the user to add data and then have it be saved. I want it to be saved if its numbers or words so if anyone has any suggestions or ideas to give me on how to do this please let me know or point me in the right direction please let me know.

list = [1, 2, 3, 4]
bon = str(next(reversed(list)))
print(bon)
a = input("new: ")
if a == 'new':
  bag = bon + 1
question from:https://stackoverflow.com/questions/66058587/saving-data-with-lists-and-appending-data-to-list-as-safety-net-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You will need some time of database or storage. There are a few ways to do this.

  1. You can create a CSV file (or a flat file with a custom delimiter). Each time your program is opened read from the CSV file and put the data in a list. Vice-versa you can just write new lines and save the file when new data is added. Although this is simple to begin with it can get even more complicated it you wish to do things like update data later on.
  2. You can use a database. The easiest one to get started with is sqlite which creates a local DB file and doesn't need database software.
  3. You can go further and use something like pickle but I can't see this being useful or the best use of your time in this case.

Let me know if this helped and if you have any questions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...