| | |
get week list of an year
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Mar 2006
Posts: 18
Reputation:
Solved Threads: 0
Python Syntax (Toggle Plain Text)
#Auther : vivek sharma #date : 06-11-2006 #Description : This script is used to get the list of week in given year, user have to input the year , def WeekFinderFromYear(year): """ will return all the week from selected year """ import datetime WEEK = {'MONDAY':0,'TUESDAY':1,'WEDNESDAY':2,'THURSDAY':3,'FRIDAY':4,'SATURDAY':5,'SUNDAY':6} MONTH = {'JANUARY':1,'FEBRUARY':2,'MARCH':3,'APRIL':4,'MAY':5,'JUNE':6,'JULY':7,'AUGUST':8,'SEPTEMBER':9,'OCTOBER':10,'NOVEMBER':11,'DECEMBER':12} year=int(year) month= MONTH['JANUARY'] day=WEEK['MONDAY'] dt = datetime.date(year,month,1) dow_lst = [] while dt.weekday() != day: dt = dt + datetime.timedelta(days=1) lst_month = MONTH.values() lst_month.sort() for mont in lst_month: while dt.month == mont: dow_lst.append(dt) dt = dt + datetime.timedelta(days=7) #for each in dow_lst: # print each return dow_lst year=raw_input("Enter the year:") week_list = WeekFinderFromYear(year) for each in week_list: print each
Last edited by stymiee; Jan 17th, 2007 at 1:37 pm. Reason: added code tags
Best wishes,
Vivek Sharma
Zope/Python Developer
Vivek Sharma
Zope/Python Developer
•
•
Join Date: Sep 2005
Posts: 133
Reputation:
Solved Threads: 58
So your script prints out all the mondays of a year, right?
Here's an easier version (well, maybe not really easier, but with less lines
)
PS: I love list comprehensions
EDIT: You may have to write
Here's an easier version (well, maybe not really easier, but with less lines
) Python Syntax (Toggle Plain Text)
import calendar year = int(raw_input("Enter year: ")) for month in range(1, 13): mondays = [ day.split()[0] for day in calendar.month(year, month).split("\n")[2:-1] if not day.startswith(" ")] for monday in mondays: print "%i-%02i-%02i" % (year, int(month), int(monday))
PS: I love list comprehensions

EDIT: You may have to write
calendar.setfirstweekday(0) to make Monday the first day in the week. What is nice is that if you e.g. want to know all the Tuesdays of a year, just write calendar.setfirstweekday(1). Last edited by mawe; Jan 19th, 2007 at 12:20 pm.
![]() |
Similar Threads
- Schedule Repeat events (PHP)
- Motherboard Resources has no driver. Audio not working (Motherboards, CPUs and RAM)
- 100% CPU usage & hardware interrupts.......2 (Viruses, Spyware and other Nasties)
- week from given year (Python)
- Sending invitation emails (Advertising Sales Strategies)
- Difference between 2 dates by month (MS Access and FileMaker Pro)
Other Threads in the Python Forum
- Previous Thread: python code for opening the web browser
- Next Thread: a simple notepad in wxPython
Views: 3096 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for Python
approximation array beginner book builtin change cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format ftp function gui homework import inches input java library line lines linux list lists loop microcontroller mouse mysqldb mysqlquery newb number numbers output parsing path plugin port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect remote script scrolledtext search singleton socket sqlite ssh string strings strip subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode urllib urllib2 variable vigenere wikipedia windows wxpython xlwt






