|
View:
New views
7 Messages
—
Rating Filter:
Alert me
|
|
|
|
|
|
Re: Retrieving information from a plain text file (WinXP/py2.6.2/Beginner)On Sun, Nov 1, 2009 at 5:37 PM, Katt <the_only_katala@...> wrote: Hello all, 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 _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: Retrieving information from a plain text file (WinXP/py2.6.2/Beginner)Hi Katt,
It appears you did not return the list of reminders that you extracted in the "read_reminders" function, but simply printed them from inside that function. If you modify your code as below to store the list in a variable called "reminders", you should be able to access the list in your global namespace. > 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 return reminders Also, on a side note, you can greatly improve the readability of your code by using the triple-quote style for multi-line docstrings inside functions (rather than the hash comment marks). I tend to use hash marks for one-line/inline comments, since they can really become an eyesore (at least IMHO) when used too liberally. Also, Python's whitespace and code formatting conventions can handle a lot of the "documentation" for you. For instance, module imports are typically always performed at the top of a script, so it's reasonable to expect that others reading your code will understand you're importing some modules. Much of this spelled out in PEP's 8 (style guide) and 257 (doc strings): http://www.python.org/dev/peps/pep-0008/ http://www.python.org/dev/peps/pep-0257/ HTH! Serdar _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: Retrieving information from a plain text file (WinXP/py2.6.2/Beginner)(Looks like maybe you hijacked another thread, instead of just creating
a new message, with new topic, for the list) Katt wrote: > <div class="moz-text-flowed" style="font-family: -moz-fixed">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)]) > > # > 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]: > 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 "Whoops, you missed the following > reminder.",reminders[index][0],"on",reminders[index][1] > change_to_julian(reminders[index][1]) > else: > 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 > > done, those reminder items are gone. It printed them, then forgot them. Similarly, compare_reminders() tries to work on reminders, when it was not passed the data either. You need to add a return statement to read_reminders(), and when you call it, you need to save it somewhere. Then you can pass it to the compare_reminders() function so it has something to compare with. Once you get that sorted out, another bug that's already apparent is that you're trying to split the line on quotes, when it uses commas between fields on each line. DaveA _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
Re: Retrieving information from a plain text file(WinXP/py2.6.2/Beginner)"Serdar Tumgoren" <zstumgoren@...> wrote > Also, on a side note, you can greatly improve the readability of your > code by using the triple-quote style for multi-line docstrings inside > functions (rather than the hash comment marks). I tend to use hash > marks for one-line/inline comments, since they can really become an > eyesore (at least IMHO) when used too liberally. I'd second that suggestion with the added benefit that if you use docstrings they will be detected by Pythons help() function. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
|
|
|
|
|
Re: Retrieving information from a plain text file(WinXP/py2.6.2/Beginner)"Katt" <the_only_katala@...> wrote >> (Looks like maybe you hijacked another thread, instead of just creating >> a new message, with new topic, for the list) > > Sorry, about that. I got lazy and just replied to a tutor message I had > in my inbox. Will make sure not to let that happen again. Looks like it happened again :-) This post shows up in my newsreader as being part of the thread about Evaluating a String Expression. In this case you should have used Reply tonyour original message then it would have been added to that thread. The reason this is important (from the posters point of view) is that if your message goes into the back of an existing thread many readers may not bother looking at it since they have lost interest in the older thread. If you want your message to be seen post a new thread for a new topic, and reply to an existing thread when deakling with that topic (so that only those following the thread need to read that one). Its a win-win scenario :-) Alan G _______________________________________________ Tutor maillist - Tutor@... To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor |
| Free embeddable forum powered by Nabble | Forum Help |