2011-09-13 113 views
1

按照主持人的建議重新對我的問題進行分詞。生成日曆Python網頁

我需要建立與Python & CSS的網頁日曆,我嘗試在Python如下:

#!/usr/bin/env python 

import os, re, sys, calendar 
from datetime import datetime 

myCal = calendar.monthcalendar(2011,9) 
html += str(myCal) 
mycal = myCal[:1] 
cal1 = myCal[1:2] 
cal2 = myCal[2:3] 
cal3 = myCal[3:4] 
cal4 = myCal[4:5] 

html += str(mycal)+'<br>' 
html += str(cal1)+'<br>' 
html += str(cal2)+'<br>' 
html += str(cal3)+'<br>' 
html += str(cal4)+'<br>' 
html += "<br>" 

這是網頁上的輸出如下:

[[0, 0, 0, 1, 2, 3, 4]]<br> 
[[5, 6, 7, 8, 9, 10, 11]]<br> 
[[12, 13, 14, 15, 16, 17, 18]]<br> 
[[19, 20, 21, 22, 23, 24, 25]]<br> 
[[26, 27, 28, 29, 30, 0, 0]]<br> 

如何按以下格式安排上述內容? ( 這是一個樣本格式,我沒有做過實際的日/日期匹配。 格式需要在兩排。
如:

日期
imgN

日期
imgN
下個月 )

     Dec , 2011 
---------------------------------------------------------------------------- 
sun | mon | tue | wed thu fri sat sun mon tue wed thu fri sat sun 
-------|-------|-------|---------------------------------------------------- 
.... 1 |.. 2 ..|.. 3 ..| 4 5 6 7 8 9 10 11 12 13 14 15<br> 
------ |-------|-------|---------------------------------------------------- 
img1 | img2 | img3 | .... 

         Jan , 2012 
---------------------------------------------------------------------------- 
sun | mon | tue | wed thu fri sat sun mon tue wed thu fri sat sun 
-------|-------|-------|---------------------------------------------------- 
.... 1 |.. 2 ..|.. 3 ..| 4 5 6 7 8 9 10 11 12 13 14 15<br> 
------ |-------|-------|---------------------------------------------------- 
img1 | img2 | img3 | .... 

         Feb , 2012 
---------------------------------------------------------------------------- 
sun | mon | tue | wed thu fri sat sun mon tue wed thu fri sat sun 
-------|-------|-------|---------------------------------------------------- 
.... 1 |.. 2 ..|.. 3 ..| 4 5 6 7 8 9 10 11 12 13 14 15<br> 
------ |-------|-------|---------------------------------------------------- 
img1 | img2 | img3 | .... 

回答

1

我不是很確定我明白你想要的確切格式,但你可以把壓延機放到3行的表格中。

試試這爲您的每個月份

import calendar 

myCal = calendar.monthcalendar(2011,9) 


#make multi rowed month into a single row list 
days = list() 
for x in myCal: 
    days.extend(x) 


#match this up with the week names with enough weeks to cover the month 
weeks = len(myCal) * ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'] 

#some images 
images = ['image1', 'image2', '...'] 

#make sure there is at least a zero at the end of the list 
days.append(0) 
#find start and end indexes where the actual day numbers lie 
start_index = days.index(1) 
#if there are no zeros at the end of the list this will fail 
end_index = days[start_index:].index(0) + len(days) - len(days[start_index:]) 

header = 'Dec, 2011' 
#Create the table rows of just the items between start_index and end_index. 
weekday_row = '<tr>%s</tr>'%(''.join(['<td>%s</td>'%(x) 
             for x in weeks[start_index:end_index]])) 
day_row = '<tr>%s</tr>'%(''.join(['<td>%d</td>'%(x) 
            for x in days[start_index:end_index]])) 
image_row = '<tr>%s</tr>'%(''.join(['<td>%s</td>' % (x) for x in images])) 


#finally put them all together to form your month block 
html = '<div>%s</div><table>\n\n%s\n\n%s\n\n%s</table>' % (header, 
                  weekday_row, 
                  day_row, 
                  image_row) 

可以重複多次,因爲你需要

3

我不知道你在尋找什麼,這將產生一個表與3行描述,但整個月(而不僅僅是前15天)。我可能是起點

import calendar 
import itertools 

blank = "&nbsp;" 

myCal = calendar.monthcalendar(2011,9) 
day_names = itertools.cycle(['mon','tue','wed','thu','fri','sat','sun']) # endless list 
cal = [day for week in myCal for day in week] # flatten list of lists 

# empty lists to hold the data 
headers = [] 
numbers = [] 
imgs = [] 

# fill lists 
for d in cal: 
    if d != 0: 
     headers.append(day_names.next()) 
     numbers.append(d) 
     imgs.append("image"+str(d)) 
    else: 
     headers.append(day_names.next()) 
     numbers.append(blank) 
     imgs.append(blank) 

# format data 
html = "<table><tr></tr>{0}<tr>{1}</tr><tr>{2}</tr></table>".format(
    "".join(["<td>%s</td>"% h for h in headers]), 
    "".join(["<td>%s</td>"% n for n in numbers]), 
    "".join(["<td>%s</td>"% i for i in imgs]))