1

how can i save the data on the list so that i can obtain the previously saved data each time i start the program this is the code:

# trial code for to do list tdl=list() print(*tdl,sep="\n") while True: choice=input("Do you want to edit the list(y/n):") if choice =="y": print("1. Do you want to add an entry") print("2. Do you want to remove an entry") choice2=input("What do you want to do(1/2):") if choice2 == '1': x=input("Enter new entry:") tdl.append(x) print(*tdl,sep="\n") if choice2 =='2': y=int(input("Enter the number of entry you want to remove:")) z=y-1 tdl.pop(z) print(*tdl,sep="\n") if choice=="n": break else: print("Invalid Entry") 
3
  • Save as Json/pickle and load it at the start of the program Commented Jan 29, 2022 at 9:01
  • Do you want to save data between program executions? In that case you will need to use seconday memory (saving the list in a pickle object for example) Commented Jan 29, 2022 at 9:02
  • This might be helpful. Pick a file name, and each time the program runs see if a file with that name exists. If yes, initialize list from the file. Save your current list before exiting the program each time. Commented Jan 29, 2022 at 9:14

2 Answers 2

1

The easiest way would be to save it in a file as a json.

# trial code for to do list tdl=list() # Reading from file with open("listentries.txt", "r") as file: tdl = json.loads(file.read()) print(*tdl,sep="\n") while True: choice=input("Do you want to edit the list(y/n):") if choice =="y": print("1. Do you want to add an entry") print("2. Do you want to remove an entry") choice2=input("What do you want to do(1/2):") if choice2 == '1': x=input("Enter new entry:") tdl.append(x) print(*tdl,sep="\n") if choice2 =='2': y=int(input("Enter the number of entry you want to remove:")) z=y-1 tdl.pop(z) print(*tdl,sep="\n") if choice=="n": # Saving to file with open("listentries.txt", "w") as file: file.write(json.dumps(tdl)) break 

I modified your code so that every time you start the program the information is loaded from the file and every time you stop the application it is saved into the file.

What do the codelines accomplish:

with open("listentries.txt"), "r") as file: It opens or creates the listentries.txt file in read mode ("r") and returns an file-object with the name file. The with statement ist a so called context manager for a deeper dive take a look here

tdl = json.loads(file.read()): I decided to store your information in JSON-Format which is a standardized way to safe or send data (find more here). So file.read() reads the information from the file and makes it accesible. This is then handed to the json.loads which allows you to convert strings into lists or dictionaries.

with open("listentries.txt", "w") as file: It does the same as above with the difference that it opens the file in order to write into it, thus "w" is handed as a parameter instead of "r"

file.write(json.dumps(tdl)): It does the same as above just in reverse. Now you have a list which you would like to write into a file. So we use json.dumps to convert it to a string in json-format and then write this into the file.

I think this should do it for you

Sign up to request clarification or add additional context in comments.

1 Comment

You might want to check if "listentries.txt" exists before attempting to read a file, e.g. using os.path.exists.
1

I would like to suggest persisting your data to an excel or some sort of file will be good since this is a console-based application. read excel using xlrd module write data into excel using xlwtmodule

1 Comment

In this case I think pickle or even excel is a better option

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.