[Tutor] Retrieving information from a plain text file (WinXP/py2.6.2/Beginner)
vince spicer
vinces1979 at gmail.com
Mon Nov 2 20:30:34 CET 2009
On Sun, Nov 1, 2009 at 5:37 PM, Katt <the_only_katala at verizon.net> wrote:
> Hello all,
>
> Thank you all for your help. I appreciate it alot.
>
> I have been trying to work with file IO alot recently and would like to
> improve my little program so that I no longer use a hard coded list, but a
> text file that I can edit easily.
>
> The text file is three lines long and looks exactly like this:
>
> Reminder1,2009_10_28
> Reminder2,2009_11_01
> Reminder3,2009_11_15
>
> My program consists of the following code:
> ============
> #]------------------[import modules]------------------[
> from time import strftime, mktime, localtime
> from WConio import textcolor
> #]--------------------------------------------------------[
> #]------------------[define functions]------------------[
> def read_reminders():
> print "\nReading text file into program: reminders.txt"
> text_file = open("reminders.txt","r")
> reminders = [line.strip().split("'") for line in text_file]
> text_file.close()
> print reminders
> #
> def get_computer_date():
> #Get today's date from the computer
> todays_date = strftime("%Y_%m_%d")
> return todays_date
> #
> def color_print(strings):
> #Change the text color in the WinXP dos shell
> #The way to use:
> #color_print([("string",color number),\
> #(str(variable),color number),(etc)])
> for string in strings:
> textcolor(string[1])
> print string[0],
> #
> def change_to_julian(reminder_date):
> #Receives the year, month, and day
> #in the form of a single string (2009_10_15)
> #and changes it into three different int
> #variables. Then take those three variables
> #and append six zeros and change into a
> #julian date.
> date = []
> date = reminder_date.split("_")
> year = int(date[0])
> month = int(date[1])
> day = int(date[2])
> timetuple = (year, month, day) + ( (0,) * 6 )
> unixtime = mktime(timetuple)
> timetuple = localtime(unixtime)
> print days_left(timetuple[7])
> # [7] is the number of julian-date field of
> #the unixtime tuple.
> return days_left(timetuple[7])
> #
> def days_left(julian_date):
> #This function calculates the days left
> #until a reminder. If the days left are
> #greater than 0 it will print normally.
> #If it is -1 then it will print differently.
> #Also if it is greater than -1 it will print
> #yet again differently.
> days_until_reminder = julian_date - localtime().tm_yday
> if days_until_reminder > 0:
> color_print ([("There are",7),(str(days_until_reminder),4),("days
> left until this reminder.",7),("\n",7)])
> elif days_until_reminder == -1:
> color_print ([("\tYou have missed this reminder
> by",4),(str(days_until_reminder*-1),4),("day!",4),("\n",7)])
> color_print [("
> ------------------------------------------------------------------------",4),("\n",7)])
> else:
> color_print ([("\tYou have missed this reminder
> by",4),(str(days_until_reminder*-1),4),("days!",4),("\n",7)])
> color_print [("
> ------------------------------------------------------------------------",4),("\n",7)])
> print
> #
> def compare_reminders(todays_date):
> #This function compares the reminders
> #to the computer date.
> #It has three different paths:
> # 1.Matches today's date
> # 2.The reminder date has already
> # passed by
> # 3.The reminder date is yet to
> # come.
> #After determining which it is it will
> #access the change_to_julian and
> #days_left functions.
> #reminders.sort()
> color_print ([("
> [-------------------------------------------------------------------------]",4),("\n",7)])
> index = 0
> while index < len(reminders):
> if todays_date == reminders[index][1]:
> print
> color_print [("
> ------------------------------------------------------------------------",4),("\n",7)])
> print "Today's reminder is:
> ",reminders[index][0],"on",reminders[index][1]
> color_print ([("\t\tTake care of this reminder
> immediately",2),("\n",7)])
> elif todays_date > reminders[index][1]:
> print
> print "Whoops, you missed the following
> reminder.",reminders[index][0],"on",reminders[index][1]
> change_to_julian(reminders[index][1])
> else:
> print
> print "Your upcoming reminders are:
> ",reminders[index][0],"on",reminders[index][1]
> change_to_julian(reminders[index][1])
> index = index + 1
> color_print ([("
> [-------------------------------------------------------------------------]",4),("\n",7)])
> #]--------------------------------------------------------[
> #]-------------------[Main Program]-------------------[
> read_reminders()
> print reminders
> compare_reminders(get_computer_date())
> pause_it = raw_input("Press a key to end: ")
> #]--------------------------------------------------------[
> ============
> Could someone explain to me why my read_reminders function retrieves the
> information, but cannot process that information?
>
> When I try and run the program I get the following error message:
> ============
> Reading text file into program: reminders.txt
> [['Reminder1,2010_10_15'], ['Reminder2,2010_11_01'],
> ['Reminder3,2010_11_15']]
> Traceback (most recent call last):
> File "reminders.py", line 182, in <module>
> print reminders
> NameError: name 'reminders' is not defined
> ============
>
> Thanks in advance for your help,
>
> Katt
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
reminders is a local variable which is only available within you
read_reminders function
you can return the results instead to be used elsewhere
EX:
def read_reminders():
print "\nReading text file into program: reminders.txt"
text_file = open("reminders.txt","r")
reminders = [line.strip().split("'") for line in text_file]
text_file.close()
return reminders
#]-------------------[Main Program]-------------------[
reminders = read_reminders()
print reminders
compare_reminders(get_computer_date())
pause_it = raw_input("Press a key to end: ")
#]--------------------------------------------------------[
Vince
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091102/f54b06b8/attachment.htm>
More information about the Tutor
mailing list