生成日历Python网页

2024-10-01 17:32:19 发布

您现在位置: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>"

以下是网页上的输出:

^{pr2}$

我怎样才能按照下面的格式安排上面的内容? ( 这是一个样本格式,我没有做实际的日期/日期匹配。 格式必须为两行。
例如







下个月 )在

                      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  | ....

Tags: br网页htmlsatsunimg1img2mon
2条回答

我不太确定我是否理解您想要的确切格式,但您可以将日历放入一个三行的表格中。在

每个月都试试这个

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行的表,但整个月(不只是前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]))

相关问题 更多 >

    热门问题