I want to save my calendar app inputs to a text file and then be able to retrieve it


#1

I need help. Been trying to figure it out for a while but can manage to.

Basically - I have built the simple calendar app from one of the previous python exercises.

Now I am trying to add the input/output functionality to it but don't know how.

I want my calendar to be saved to a text file when I open the application and use it and then be able to retrieve the text file whenever I open the application again.

Here is my code:

"""
This program is a bsic calendar that allows the user to interact with from the command line. The user should be able to: view the calendar, add an event, update an event and delete an event.
Author: zreichert
"""

from time import sleep, strftime, strptime

USER_FIRST_NAME = "Ziv"

calendar = {

}

def welcome():
  print "Welcome, " + \
  USER_FIRST_NAME + "."
  print "Calendar starting..."
  sleep(1)
  print "Today is: " + \
  strftime("%A %B %d, %Y")
  print "The time is: " + \
  strftime("%H:%M:%S")
  sleep(1)
  print "What would you like to do?"

def start_calendar():
  welcome()
  start = True
  while start:
    user_choice = raw_input("A to Add, U to Update, V to View, D to Delete, X to Exit: ")
    user_choice = user_choice.upper()
    if user_choice == "V":
      if len(calendar.keys()) < 1:
        print "Calendar empty."
      else:
        print calendar
    elif user_choice.upper() == "U":
      date = raw_input("What date? ")
      update = raw_input("Enter the update: ")
      if date in calendar.keys():
        calendar[date] = update
        print "Update successful"
        print calendar
      else:
        print "Invalid date"
    elif user_choice.upper() == "A":
      event = raw_input("Enter event: ")
      date = raw_input("Enter date (MM/DD/YYYY): ")
      if (len(date) > 10) or int(date[6:]) < int(strftime("%Y")):
        print "Invalid date entered!"
        try_again = raw_input("Try Again? Y for Yes, N for No: ")
        try_again = try_again.upper()
        if try_again == "Y":
          continue
        else:
          start = False
      else:
        calendar[date] = event
    elif user_choice.upper() == "D":
      if len(calendar.keys()) < 1:
        print "Calendar empty."
      else:
        event = raw_input("What event? ")
        for date in calendar.keys():
          if event == calendar[date]:
            del calendar[date]
            print "Event deleted!"
          else:
            print "Incorrect date specified"
    elif user_choice.upper() == "X":
      start = False
    else:
      print "Invalid command entered"

start_calendar()

Can anyone help?


#2

There's an object serialization module called pickle if you want to save and load objects.

You can also just write the dictionary to the output file as a string like this

outfile.write(str(calendar))

but then you also need to read the file back as a dict, this can be done by using eval() but using it is not recommended because eval() might execute arbitrary code. There's a safer alternative that is ast.literal_eval() from the ast module.

So you need to load the file that contains your dictionary for example like this

with open("output.txt", "r") as infile:
    imported = ast.literal_eval(infile.read())

and from there you only need to copy the content from imported to calendar.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.