943,162 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2117
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 13th, 2010
0

Converting raw input and printing lists from file to new lines.

Expand Post »
I am new to programming in Python and I am just wondering if I could have some help.

I am trying to write a program which creates diary entries for events and I am currently trying to convert the raw_input from the user into a date (yyyy/mm/dd) and time (24hr hh:mm). I have tried looking around for a solution but nothing I do seems to work.

I am also trying to append each event to file and display them on separate lines when they are loaded from the file.

Here is my code up to now:


Python Syntax (Toggle Plain Text)
  1. import cPickle
  2.  
  3. def print_menu():
  4. print '1. Add an Event'
  5. print '2. Save Events to File'
  6. print '3. Load Events from File'
  7. print '4. Quit'
  8. print
  9.  
  10. event_list = []
  11. menu_choice = 0
  12. print_menu()
  13. while True:
  14. menu_choice = input("Select Menu Item (1-4): ")
  15. if menu_choice == 1:
  16. print "Add Event"
  17. date = input("Date: ")
  18. time = raw_input("Time: ")
  19. title = raw_input("Title: ")
  20. desc = raw_input("Description: ")
  21. loc = raw_input("Location: ")
  22. attend = raw_input("Attendee(s): ")
  23. ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend
  24. event_list.append(ui)
  25. elif menu_choice == 2:
  26. filename = raw_input("Filename to save: ")
  27. pickle_file = open(filename, "w")
  28. cPickle.dump(event_list, pickle_file)
  29. pickle_file.close()
  30. elif menu_choice == 3:
  31. filename = raw_input("Filename to load: ")
  32. pickle_file = open(filename, "r")
  33. events = cPickle.load(pickle_file)
  34. pickle_file.close()
  35. print "Events:", events
  36. elif menu_choice == 4:
  37. break
  38. else:
  39. print_menu()
  40.  
  41. print "Goodbye"

Any help would be greatly appreciated!
Last edited by vegaseat; Mar 13th, 2010 at 5:01 pm. Reason: added codetags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
emma.parsons is offline Offline
13 posts
since Mar 2010
Mar 13th, 2010
0
Re: Converting raw input and printing lists from file to new lines.
Your code looks like it's running fine to me. Would you be able to edit your post and put tags around the code? It's hard to read. I also don't know what exactly you want. It's definitely taking the input how you want it. Perhaps just ask the user to put it in the format dd/mm/yy?
Reputation Points: 35
Solved Threads: 18
Junior Poster
hondros is offline Offline
147 posts
since Nov 2009
Mar 13th, 2010
0
Re: Converting raw input and printing lists from file to new lines.
Click to Expand / Collapse  Quote originally posted by hondros ...
Your code looks like it's running fine to me. Would you be able to edit your post and put tags around the code? It's hard to read. I also don't know what exactly you want. It's definitely taking the input how you want it. Perhaps just ask the user to put it in the format dd/mm/yy?


Sorry I forgot to add the tags.

The code I have provided does run however I now need to format the date and time so when a user enters the value as text it is converted into the format yyyy/mm/dd and hh:mm. Another problem I have at the minute is when more than 1 event is added and saved to file, when it is loaded each event is displayed all on one line and the separate events need to be on separate lines with the 6 field titles (date, time, title, description, location, attendees) displayed also. (I hope this makes sense)

Here is my code, hope it's now clearer:

Python Syntax (Toggle Plain Text)
  1. import cPickle
  2.  
  3. def print_menu():
  4. print '1. Add an Event'
  5. print '2. Save Events to File'
  6. print '3. Load Events from File'
  7. print '4. Quit'
  8. print
  9.  
  10. event_list = []
  11. menu_choice = 0
  12. print_menu()
  13. while True:
  14. menu_choice = input("Select Menu Item (1-4): ")
  15. if menu_choice == 1:
  16. print "Add Event"
  17. date = input("Date: ")
  18. time = raw_input("Time: ")
  19. title = raw_input("Title: ")
  20. desc = raw_input("Description: ")
  21. loc = raw_input("Location: ")
  22. attend = raw_input("Attendee(s): ")
  23. ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend
  24. event_list.append(ui)
  25. elif menu_choice == 2:
  26. filename = raw_input("Filename to save: ")
  27. pickle_file = open(filename, "w")
  28. cPickle.dump(event_list, pickle_file)
  29. pickle_file.close()
  30. elif menu_choice == 3:
  31. filename = raw_input("Filename to load: ")
  32. pickle_file = open(filename, "r")
  33. events = cPickle.load(pickle_file)
  34. pickle_file.close()
  35. print "Events:", events
  36. elif menu_choice == 4:
  37. break
  38. else:
  39. print_menu()
  40.  
  41. print "Goodbye"
Reputation Points: 10
Solved Threads: 0
Newbie Poster
emma.parsons is offline Offline
13 posts
since Mar 2010
Mar 13th, 2010
0
Re: Converting raw input and printing lists from file to new lines.
Here you go. I still don't know how the user will be inputting data, so I included two choices for you. Just get rid of the one you don't want. I was half contemplating on getting rid of whitespace to make you tab each one like you did, but I left it in to be nice =]
Python Syntax (Toggle Plain Text)
  1. import cPickle
  2.  
  3. def print_menu():
  4. print '1. Add an Event'
  5. print '2. Save Events to File'
  6. print '3. Load Events from File'
  7. print '4. Quit'
  8. print()
  9.  
  10. event_list = []
  11. menu_choice = 0
  12. print_menu()
  13. while True:
  14. menu_choice = input("Select Menu Item (1-4): ")
  15. if menu_choice == 1:
  16. print "Add Event"
  17. date = input("Date: ")
  18. # yyyy/mm/dd
  19. # Assuming that the user inputs this: 20100313
  20. date = date[0:4]+"/"+date[4:6]+"/"+date[6:8]
  21. # Assuming that the user inputs this: 2010, 3, 13
  22. date = date.split(', ')
  23. if len(date[0]) == 2:
  24. date[0] = '20'+date[0]
  25. if len(date[1]) == 1:
  26. date[1] = '0'+date[1]
  27. if len(date[2]) == 1:
  28. date[2] = '0'+date[2]
  29. date = '/'.join(date)
  30. #
  31. time = raw_input("Time: ")
  32. title = raw_input("Title: ")
  33. desc = raw_input("Description: ")
  34. loc = raw_input("Location: ")
  35. attend = raw_input("Attendee(s): ")
  36. #ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend
  37. # Add a newline to seperate each entry
  38. ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend + '\n'
  39. event_list.append(ui)
  40. elif menu_choice == 2:
  41. filename = raw_input("Filename to save: ")
  42. pickle_file = open(filename, "w")
  43. cPickle.dump(event_list, pickle_file)
  44. pickle_file.close()
  45. elif menu_choice == 3:
  46. filename = raw_input("Filename to load: ")
  47. pickle_file = open(filename, "r")
  48. events = cPickle.load(pickle_file)
  49. pickle_file.close()
  50. print "Events:", events
  51. elif menu_choice == 4:
  52. break
  53. else:
  54. print_menu()
  55.  
  56. print "Goodbye"
Reputation Points: 35
Solved Threads: 18
Junior Poster
hondros is offline Offline
147 posts
since Nov 2009
Mar 13th, 2010
0
Re: Converting raw input and printing lists from file to new lines.
Click to Expand / Collapse  Quote originally posted by hondros ...
Here you go. I still don't know how the user will be inputting data, so I included two choices for you. Just get rid of the one you don't want. I was half contemplating on getting rid of whitespace to make you tab each one like you did, but I left it in to be nice =]
Python Syntax (Toggle Plain Text)
  1. import cPickle
  2.  
  3. def print_menu():
  4. print '1. Add an Event'
  5. print '2. Save Events to File'
  6. print '3. Load Events from File'
  7. print '4. Quit'
  8. print()
  9.  
  10. event_list = []
  11. menu_choice = 0
  12. print_menu()
  13. while True:
  14. menu_choice = input("Select Menu Item (1-4): ")
  15. if menu_choice == 1:
  16. print "Add Event"
  17. date = input("Date: ")
  18. # yyyy/mm/dd
  19. # Assuming that the user inputs this: 20100313
  20. date = date[0:4]+"/"+date[4:6]+"/"+date[6:8]
  21. # Assuming that the user inputs this: 2010, 3, 13
  22. date = date.split(', ')
  23. if len(date[0]) == 2:
  24. date[0] = '20'+date[0]
  25. if len(date[1]) == 1:
  26. date[1] = '0'+date[1]
  27. if len(date[2]) == 1:
  28. date[2] = '0'+date[2]
  29. date = '/'.join(date)
  30. #
  31. time = raw_input("Time: ")
  32. title = raw_input("Title: ")
  33. desc = raw_input("Description: ")
  34. loc = raw_input("Location: ")
  35. attend = raw_input("Attendee(s): ")
  36. #ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend
  37. # Add a newline to seperate each entry
  38. ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend + '\n'
  39. event_list.append(ui)
  40. elif menu_choice == 2:
  41. filename = raw_input("Filename to save: ")
  42. pickle_file = open(filename, "w")
  43. cPickle.dump(event_list, pickle_file)
  44. pickle_file.close()
  45. elif menu_choice == 3:
  46. filename = raw_input("Filename to load: ")
  47. pickle_file = open(filename, "r")
  48. events = cPickle.load(pickle_file)
  49. pickle_file.close()
  50. print "Events:", events
  51. elif menu_choice == 4:
  52. break
  53. else:
  54. print_menu()
  55.  
  56. print "Goodbye"



I like the method for the date input assuming the user enters 2010, 03, 13 but (I dont know whether it's me) it doesn't seem to be working. I keep getting the error:
date = date.split(', ')
AttributeError: 'tuple' object has no attribute 'split'

and for the adding of a new line to separate each entry, I have the following error:
TypeError: cannot concatenate 'str' and 'int' objects
Reputation Points: 10
Solved Threads: 0
Newbie Poster
emma.parsons is offline Offline
13 posts
since Mar 2010
Mar 13th, 2010
0
Re: Converting raw input and printing lists from file to new lines.
Had to get rid of the commented part of code (First block), then change date = input() to date = raw_input(). You still have to change the way the event load presents itself. But I fixed the major part for you:
Python Syntax (Toggle Plain Text)
  1. import cPickle
  2.  
  3. def print_menu():
  4. print '1. Add an Event'
  5. print '2. Save Events to File'
  6. print '3. Load Events from File'
  7. print '4. Quit'
  8. print()
  9.  
  10. event_list = []
  11. menu_choice = 0
  12. print_menu()
  13. while True:
  14. menu_choice = input("Select Menu Item (1-4): ")
  15. if menu_choice == 1:
  16. print "Add Event"
  17. date = raw_input("Date: ")
  18. # yyyy/mm/dd
  19. # Assuming that the user inputs this: 2010, 3, 13
  20. date = date.split(', ')
  21. if len(date[0]) == 2:
  22. date[0] = '20'+date[0]
  23. if len(date[1]) == 1:
  24. date[1] = '0'+date[1]
  25. if len(date[2]) == 1:
  26. date[2] = '0'+date[2]
  27. date = '/'.join(date)
  28. #
  29. time = raw_input("Time: ")
  30. title = raw_input("Title: ")
  31. desc = raw_input("Description: ")
  32. loc = raw_input("Location: ")
  33. attend = raw_input("Attendee(s): ")
  34. #ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend
  35. # Add a newline to seperate each entry
  36. ui = "Date: " + date, "Time: " + time, "Title: " + title, "Description: " + desc, "Location: " + loc, "Attendee(s): " + attend + '\n'
  37. event_list.append(ui)
  38. elif menu_choice == 2:
  39. filename = raw_input("Filename to save: ")
  40. pickle_file = open(filename, "w")
  41. cPickle.dump(event_list, pickle_file)
  42. pickle_file.close()
  43. elif menu_choice == 3:
  44. filename = raw_input("Filename to load: ")
  45. pickle_file = open(filename, "r")
  46. events = cPickle.load(pickle_file)
  47. pickle_file.close()
  48. print "Events:", events
  49. elif menu_choice == 4:
  50. break
  51. else:
  52. print_menu()
  53.  
  54. print "Goodbye"

My output:
Python Syntax (Toggle Plain Text)
  1. 1. Add an Event
  2. 2. Save Events to File
  3. 3. Load Events from File
  4. 4. Quit
  5. ()
  6. Select Menu Item (1-4): 1
  7. Add Event
  8. Date: 2010, 3, 13
  9. Time: 22 13 3
  10. Title: Here
  11. Description: No
  12. Location: Here
  13. Attendee(s): It
  14. Select Menu Item (1-4): 2
  15. Filename to save: Here.txt
  16. Select Menu Item (1-4): 3
  17. Filename to load: Here.txt
  18. Events: [('Date: 2010/03/13', 'Time: 22 13 3', 'Title: Here', 'Description: No', 'Location: Here', 'Attendee(s): It\n')]
  19. Select Menu Item (1-4):
Last edited by hondros; Mar 13th, 2010 at 3:50 pm. Reason: My output
Reputation Points: 35
Solved Threads: 18
Junior Poster
hondros is offline Offline
147 posts
since Nov 2009
Mar 13th, 2010
0
Re: Converting raw input and printing lists from file to new lines.
Thank you for that.

The only other bit I need help with is the loading of the events from the file. They are saving and loading all of the information but when they display they're all on one line with square brackets and commas separating them and I need each event to be on a separate line without the square brackets and commas between each field
It is currently displaying like this:

Events: [('Date: 2010/03/13', 'Time: 2am', 'Title: emma', 'Description: test', 'Location: home', 'Attendee(s): emma, sam\n'), ('Date: 2010/03/26', 'Time: 4pm', 'Title: ebiz', 'Description: deadline', 'Location: furnival', 'Attendee(s): emma\n')]

..the new line '\n' isn't working.

Sorry for all of the questions, I am very new to this as I said.

Thank You in advance!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
emma.parsons is offline Offline
13 posts
since Mar 2010
Mar 13th, 2010
0
Re: Converting raw input and printing lists from file to new lines.
Python Syntax (Toggle Plain Text)
  1. # Separate the events at the '\n' marker
  2. for x in events[0].split('\n'):
  3. # Iterate through the list of each entry
  4. for y in x:
  5. # print the result
  6. print y
Reputation Points: 35
Solved Threads: 18
Junior Poster
hondros is offline Offline
147 posts
since Nov 2009
Mar 13th, 2010
0
Re: Converting raw input and printing lists from file to new lines.
Click to Expand / Collapse  Quote originally posted by hondros ...
Python Syntax (Toggle Plain Text)
  1. # Separate the events at the '\n' marker
  2. for x in events[0].split('\n'):
  3. # Iterate through the list of each entry
  4. for y in x:
  5. # print the result
  6. print y


I'm really sorry but where should that code be placed, I just tried it within the file load but it's not working, I get an error: AttributeError: 'tuple' object has no attribute 'split'
Reputation Points: 10
Solved Threads: 0
Newbie Poster
emma.parsons is offline Offline
13 posts
since Mar 2010
Mar 13th, 2010
1
Re: Converting raw input and printing lists from file to new lines.
Okay, sorry. The tuple appears to not have a .split() function, only a list does. So, we shall go ahead and convert the tuple to a list, then iterate through the split:
Python Syntax (Toggle Plain Text)
  1. n = []
  2. for x in events:
  3. n.append(x)
  4. # Make the tuple to a list
  5. events = n[:]
  6. # NOTE:
  7. # You could just do a conversion using the list() function;
  8. # IE. events = list(events)
  9. # Less typing, but the other way you'll understand how the tuple and list works
  10. # Separate the events at the '\n' marker
  11. for x in events[0].split('\n'):
  12. # Iterate through the list of each entry
  13. for y in x:
  14. # print the result
  15. print y
Reputation Points: 35
Solved Threads: 18
Junior Poster
hondros is offline Offline
147 posts
since Nov 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: What does this...
Next Thread in Python Forum Timeline: Python+Microcontroller=Mouse





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC