你可以用日曆:如果你想
>>> import calendar
>>> print calendar.TextCalendar().formatmonth(2013,2,4)
February 2013
Mon Tue Wed Thu Fri Sat Sun
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28
星期日作爲第一天:
>>> print calendar.TextCalendar(calendar.SUNDAY).formatmonth(2013,2,4)
February 2013
Sun Mon Tue Wed Thu Fri Sat
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28
概念,日曆是一個表;和一張桌子是一個列表的列表的文本表示:
>>> table=[['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
... ['','','','','','1','2'],
... ['3','4','5','6','7','8','9'],
... ['10','11','12','13','14','15','16'],
... ['17','18','19','20','21','22','23'],
... ['24','25','26','27','28','','']]
如果你想「漂亮打印」該表爲一個日曆,你將需要推出自己的:
def printTable (tbl, borderHorizontal = '-', borderVertical = '|', borderCross = '+'):
cols = [list(x) for x in zip(*tbl)]
lengths = [max(map(len, map(str, col))) for col in cols]
f = borderVertical + borderVertical.join(' {:>%d} ' % l for l in lengths) + borderVertical
s = borderCross + borderCross.join(borderHorizontal * (l+2) for l in lengths) + borderCross
rtr=[s]
for row in tbl:
rtr.append(f.format(*row))
rtr.append(s)
return '\n'.join(rtr)
print(printTable(table))
打印:
+-----+-----+-----+-----+-----+-----+-----+
| Sun | Mon | Tue | Wed | Thu | Fri | Sat |
+-----+-----+-----+-----+-----+-----+-----+
| | | | | | 1 | 2 |
+-----+-----+-----+-----+-----+-----+-----+
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
+-----+-----+-----+-----+-----+-----+-----+
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
+-----+-----+-----+-----+-----+-----+-----+
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
+-----+-----+-----+-----+-----+-----+-----+
| 24 | 25 | 26 | 27 | 28 | | |
+-----+-----+-----+-----+-----+-----+-----+
我會,但我在介紹comp大學在我的大學。我們被要求製作一個日曆,要求一年和一天,並相應地顯示。由於我們在介紹,她期望日曆看起來像一個noobie做到了。日曆模塊對我們來說太整潔了,我們不從中學習編程。 =/ – user2877540
OK,然後我沒有幫助了...乾杯... :-) – nickie
對上!只需要開始=)不是一個答案的關鍵..這是沒有樂趣,不會幫助我一點..謝謝! – user2877540