Re: Date using input
flebber.c...@gmail.com wrote: I don't think I am using re.compile properly, but thought as this would make my output an object it would be better for later, is that correct? #Obtain date def ObtainDate(date): date = raw_input(Type Date dd/mm/year: ) re.split('[/]+', date) date year = date[-1] month = date[1] day = date[0] re.compile('year-month-day') You persist in top-posting (putting your reply ahead of the sequence of messages you're quoting). Regular expressions are an advanced technique. You need to learn how strings work, how names clash, how functions return values. Learn how the fundamental types can be manipulated before messing with datetime module, or re module. As I said in an earlier message: Do you understand what kind of data is returned by raw_input() ? If so, look at the available methods of that type, and see if there's one called split() that you can use to separate out the multiple parts of the user's response. You want to separate the dd from the mm and from the year. You've done the same with the more complex and slower re module. You could have just used the code I practically handed you: date_string = raw_input(Type Date dd/mm/year: ) date_list = datestring.split(/) Then, you have correctly parsed the list into separate string variables. Easier would have been: day, month, year = date_list or just combining the last two lines: day, month, year = date_string.split(/) Now you'd like to put them together in a different order, with a different separator. The re module doesn't do that, but again, you don't need it. Look up str.join(), which is pretty much the inverse of split, or simply look up how to concatenate strings with the + operator. Now, consider the calling and return of that function. What are you using the formal parameter date for? Why is it even there? And what value are you intending to return? Try writing one complete script, complete with a single definition, and a single call to that definition, perhaps printing the result calling that definition. Master chapters 1-3 before jumping to chapter 12. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
flebber wrote: Sorry to ask a simple question but I am a little confused how to combine the input function and the date time module. Simply at the start of the program I want to prompt the user to enter the date, desirably in the format dd/mm/year. However I want to make sure that python understands the time format because first the date will form part of the name of the output file so dd/mm/year as 1st September 2009, secondly if I have multiple output files saved in a directory I may need to search later on the files and contents of files by date range. So can I timestamp the file? I know this is a simple question but it eludes me exactly how to do it. I have the basics from http://docs.python.org/library/datetime.html from datetime import date date = input(type date dd/mm/year: ) datetime(day,month,year) # some program blocks #print to file(name = date) or apphend if it exists What version of python is your class, instructor, and text book using? If you want to learn fastest, you probably need to be using the same, or nearly same environment. The input() function is one place where it matters whether it's Python 2.x or Python 3.x. While you're at it, you should give the rest of your environment, such as which OS. The doc page you pointed us to is for Python 2.6.2, but the input function on that version returns an integer. Perhaps you want raw_input() ? What code have you written, and what about it doesn't work? Have you added print statements before the line that causes the error to see what the intermediate values are? To try to anticipate some of your problems, you should realize that in most file systems, the slash is a reserved character, so you can't write the date that way. I'd suggest using dashes. I put dates in directory names, and I always put year, then month, then day, because then sorting the filenames also sorts the dates. I'm not in a country that sorts dates that way, but it does make things easier. So directories for the last few days would be: 2009-09-22 2009-09-23 2009-09-24 When asking the user for a date, or telling him a date, by all means use your country's preferred format, as you say. You mention timestamping the file. While that can be done (Unix touch, for example), I consider it a last resort for keeping track of useful information. At best, it should be an extra reminder of something that's already in the file contents. And since many programs make the assumption that if the timestamp doesn't change, the contents haven't changed, you can only reasonably do this on a file whose contents are fixed when first created. If you control the internal format of the file, put the date there, perhaps right after the header which defines the data type and version. -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Date using input
I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. On Sep 24, 2009 9:59pm, Dave Angel da...@ieee.org wrote: flebber wrote: Sorry to ask a simple question but I am a little confused how to combine the input function and the date time module. Simply at the start of the program I want to prompt the user to enter the date, desirably in the format dd/mm/year. However I want to make sure that python understands the time format because first the date will form part of the name of the output file so dd/mm/year as 1st September 2009, secondly if I have multiple output files saved in a directory I may need to search later on the files and contents of files by date range. So can I timestamp the file? I know this is a simple question but it eludes me exactly how to do it. I have the basics from http://docs.python.org/library/datetime.html from datetime import date date = input(type date dd/mm/year: ) datetime(day,month,year) # some program blocks #print to file(name = date) or apphend if it exists What version of python is your class, instructor, and text book using? If you want to learn fastest, you probably need to be using the same, or nearly same environment. The input() function is one place where it matters whether it's Python 2.x or Python 3.x. While you're at it, you should give the rest of your environment, such as which OS. The doc page you pointed us to is for Python 2.6.2, but the input function on that version returns an integer. Perhaps you want raw_input() ? What code have you written, and what about it doesn't work? Have you added print statements before the line that causes the error to see what the intermediate values are? To try to anticipate some of your problems, you should realize that in most file systems, the slash is a reserved character, so you can't write the date that way. I'd suggest using dashes. I put dates in directory names, and I always put year, then month, then day, because then sorting the filenames also sorts the dates. I'm not in a country that sorts dates that way, but it does make things easier. So directories for the last few days would be: 2009-09-22 2009-09-23 2009-09-24 When asking the user for a date, or telling him a date, by all means use your country's preferred format, as you say. You mention timestamping the file. While that can be done (Unix touch, for example), I consider it a last resort for keeping track of useful information. At best, it should be an extra reminder of something that's already in the file contents. And since many programs make the assumption that if the timestamp doesn't change, the contents haven't changed, you can only reasonably do this on a file whose contents are fixed when first created. If you control the internal format of the file, put the date there, perhaps right after the header which defines the data type and version. -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? Make sure each of these is in a function with a good name, so you can later refine the behavior, like adding error checking. So right now, you might code the function without error checking, and later add some validation, with error messages and new input query. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date = raw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date = raw_input(Type Date dd/mm/year: ) date.format = (%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date = raw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) By setting date = raw_input(...), you mask the datetime.date object preventing you from using it in the next print line. Additionally, the - aren't used to separate parameters...you want commas. You also haven't split out the year/month/day bits to pass to the date.datetime() constructor. So immediate corrections involve: 1) choose a name other than date for the value returned from raw_input() 2) take that resulting value from step #1 and split it up so you have the constituent parts. This is a wonderful use for tuple assignment. 3) After splitting parts up, you still have strings, so you need to convert them to numbers. (for advanced users, I'd use map() to do the conversion in step #2) 4) once you have the year, month, and day values as integers, you can pass them to the datetime.date constructor (instead of the datetime.date.datetime constructor which is a little weird). #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime You'd then want to create a file-name to open, based on the date object. The strftime() method will help you here (see the docs on the format string). Once you have the filename, you'll want to open a file with that name, appending to it if it already exists (see the docs on the file() object) -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
flebber wrote: On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date =aw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date =aw_input(Type Date dd/mm/year: ) date.format =%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format As Tim says, first thing you want to do is rename that variable. You've defined two symbols with the same name. Then I'd ask what that %2.0r stuff is inside the prompt to the user. Do you understand what kind of data is returned by raw_input() ? If so, look at the available methods of that type, and see if there's one called split() that you can use to separate out the multiple parts of the user's response. You want to separate the dd from the mm and from the year. Once you've split the text, then you want to recombine it in a different order, and with dashes between the parts. If I were at your stage of experience, I'd not bother with the datetime module at all. Just see if you can rebuild the string you need, assuming the user has entered a valid 10-character string. Later you can go back and figure out the datetime logic. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Date using input
Okay, thanks for the advice that sounds a good place to start. I used %2.os was an attempt to define width and precision to stop typo errors eg the user accidentally inputing 101/09/2009 or similar error. So that the __/__/ was adhered to. I will go back to the start get the basics happening and then figure out a way to catch errors with format and correctness. Thanks On Sep 25, 2009 3:57am, Dave Angel da...@ieee.org wrote: flebber wrote: On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date =aw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date =aw_input(Type Date dd/mm/year: ) date.format =%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format As Tim says, first thing you want to do is rename that variable. You've defined two symbols with the same name. Then I'd ask what that %2.0r stuff is inside the prompt to the user. Do you understand what kind of data is returned by raw_input() ? If so, look at the available methods of that type, and see if there's one called split() that you can use to separate out the multiple parts of the user's response. You want to separate the dd from the mm and from the year. Once you've split the text, then you want to recombine it in a different order, and with dashes between the parts. If I were at your stage of experience, I'd not bother with the datetime module at all. Just see if you can rebuild the string you need, assuming the user has entered a valid 10-character string. Later you can go back and figure out the datetime logic. DaveA -- http://mail.python.org/mailman/listinfo/python-list
Re: Date using input
On Sep 24, 7:49 am, flebber flebber.c...@gmail.com wrote: On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date = raw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date = raw_input(Type Date dd/mm/year: ) date.format = (%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format I think you are looking for datetime.strptime() input = raw_input(Type Date dd/mm/year: ) d = datetime.strptime(input, %d/%m/%y) print d ~Sean -- http://mail.python.org/mailman/listinfo/python-list
Re: Re: Re: Date using input
I don't think I am using re.compile properly, but thought as this would make my output an object it would be better for later, is that correct? #Obtain date def ObtainDate(date): date = raw_input(Type Date dd/mm/year: ) re.split('[/]+', date) date year = date[-1] month = date[1] day = date[0] re.compile('year-month-day') On Sep 25, 2009 8:32am, flebber.c...@gmail.com wrote: Okay, thanks for the advice that sounds a good place to start. I used %2.os was an attempt to define width and precision to stop typo errors eg the user accidentally inputing 101/09/2009 or similar error. So that the __/__/ was adhered to. I will go back to the start get the basics happening and then figure out a way to catch errors with format and correctness. Thanks On Sep 25, 2009 3:57am, Dave Angel da...@ieee.org wrote: flebber wrote: On Sep 24, 11:10 pm, flebber flebber.c...@gmail.com wrote: On Sep 24, 10:58 pm, Dave Angel da...@ieee.org wrote: flebber.c...@gmail.com wrote: I am using python 2.6.2, I haven't updated to 3.0 yet. No I have no class or instructor, I am learning this myself. I have Hetlands book Beginning Python Novice to Professional and online documentation books so Dive into Python, python.org etc. Using the SPE editor. I have currently only fully written basic psuedocode to give me a basic framework to guide myself. #Basic pseudocode #Purpose to get raw input and calculate a score for a field of options and return that #score in a set in descending order. #Multiple sets sould be written to the doc #Obtain date #Check if txt file with same date exists. If yes apphend to results to file. #Obtain location #Set Dictionary #Event number #Obtain set size #Prompt first entry #First Entry Number #First Entry Name #Set Blocks to obtain and calculate data #Block 1 example - Placings Block #Obtain number of events competed in #Obtain how many times finished first #Ensure this value is not greater than Number of Events #Number of Firsts divide by Events * total by 15. #Obtain Second finishes #Ensure this value is not greater than Number of Events #Number of Seconds divide by Events * total by 10. #Continue On with this #Block 2 - Lookup coach Dict and apply value. #Obtain Surname of Coach #Lookup Coach File and Match Name and get value. #Blocks continue gaining and calculating values. #create txt file named using date #Sum Values Block1 + Block2 etc #Print to file event number and field with name number individual Block totals and Sum Total #Arranged in descending Sum Total. #Prompt are there any more events? Yes return to start #Apphend all additional events to same day file seperated by blank line. How many of these steps have you attempted actually coding? Seems to me your first two steps are just string manipulation, and you only need to use the datetime module if you need to validate. In other words, if the user specifies the date as 31/09/2009, you might want to later bounce back to him with a complaint that September only has 30 days. So the first task is to accept input in the form ab/cd/efgh and produce a string efgh-cd-ab.log which you will then create as a text file. And if the file exists, you'll append to it instead of overwriting it. Can you do that much? DaveA Trying but haven't got it working, thats why I started to try and use datetime module. Surely getting it tottally mixed up from datetime import date def ObtainDate(params): date =aw_input(Type Date dd/mm/year: %2.0r%2.0r/%2.0r%2.0r/%4.0r %4.0r%4.0r%4.0r) print date.datetime(year-month-day) #Check if txt file with same date exists. If yes apphend to results to file. date.append(datetime and def ObtainDate(params): date =aw_input(Type Date dd/mm/year: ) date.format =%4.0s%4.0s%4.0s%4.0s-%2.0s%2.0s-%2.0s) print date.format As Tim says, first thing you want to do is rename that variable. You've defined two symbols with the same name. Then I'd ask what that %2.0r stuff is inside the prompt to the user. Do you understand what kind of data is returned by raw_input() ? If so, look at the available methods of that type, and see if there's one called split() that you can use to separate out the multiple parts of the user's response. You want to separate the dd from the mm and from the year. Once you've split the text, then you want to recombine it in a different order, and with dashes between the parts. If I were at your stage of experience, I'd not bother with the datetime module at all. Just see if you can rebuild the string you need, assuming the user has entered a valid 10-character string. Later you can go back and figure out the datetime logic. DaveA -- http://mail.python.org/mailman/listinfo/python-list