【问题标题】:Generate calendar Python webpage生成日历 Python 网页
【发布时间】:2011-09-14 11:50:26
【问题描述】:

按照版主的建议重新表述我的问题。

我需要使用 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>

我怎样才能按照下面的格式排列上面的内容? ( 这是一个 SAMPLE 格式,我还没有完成实际的日期/日期匹配。 格式必须为两行。
例如
星期
日期
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  | ....

【问题讨论】:

    标签: python calendar webpage


    【解决方案1】:

    我不确定您要查找的确切内容,这会生成一个包含您描述的 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]))
    

    【讨论】:

      【解决方案2】:

      我不确定我是否了解您想要的确切格式,但您可以将日历放入 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)
      

      您可以根据需要重复多次

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-10
        • 2015-10-14
        • 1970-01-01
        • 2020-03-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多