Can I print 2 calendars side by side?

Tim Chase python.list at tim.thechases.com
Wed Nov 23 13:39:19 EST 2016


On 2016-11-23 10:02, Dayton Jones wrote:
> I'd like to be able to display 2 calendars side by side, instead of
> stacked... is this possible?
> 
> for instance:
> 
>     print(calendar.month(year_a,month))
>     print()
>     print(calendar.month(year_b,month))
> 
> prints:
>      June 1971
> Mo Tu We Th Fr Sa Su
>     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 29 30
> 
> 
>      June 2017
> Mo Tu We Th Fr Sa Su
>           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 29 30
> 
> 
> but what I would like is:
>      June 1971               June 2017
> Mo Tu We Th Fr Sa Su    Mo Tu We Th Fr Sa Su
>     1  2  3  4  5  6              1  2  3  4
>  7  8  9 10 11 12 13     5  6  7  8  9 10 11
> 14 15 16 17 18 19 20    12 13 14 15 16 17 18
> 21 22 23 24 25 26 27    19 20 21 22 23 24 25
> 28 29 30                26 27 28 29 30

This seems to do the trick for me

  from itertools import izip_longest
  print ('\n'.join(
    "%-24s %s" % (week1, week2)
    for week1, week2
    in izip_longest(
      month(1971, 6).splitlines(),
      month(2017, 6).splitlines(),
      fillvalue='',
      )
    ))

Adjust the "-24" to put your desired amount of padding between the
calendars (needs to be at least 21=3 character-columns per day * 7
days per week).  If you want more than two weeks, you can generalize
it:

  dates = [
    month(1971, 6),
    month(2001, 6),
    month(2017, 6),
    ]

  print ('\n'.join(
    '  '.join(w.ljust(21) for w in weeks)
    for weeks
    in izip_longest(
      *[cal.splitlines() for cal in dates],
      fillvalue=''
      )
    ))

Hope this helps,

-tkc






More information about the Python-list mailing list