Can I print 2 calendars side by side?
MRAB
python at mrabarnett.plus.com
Wed Nov 23 14:01:43 EST 2016
On 2016-11-23 18: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
>
The functions return strings, so you can split them into lines:
rows_a = calendar.month(year_a, month).splitlines()
rows_b = calendar.month(year_b, month).splitlines()
You can then zip them together:
rows = ['{} {}'.format(row_a, row_b) for row_a, row_b in zip(rows_a,
rows_b)]
However:
1. The 2 months might have a different number of rows. 'zip' will
truncate to the shorter one, so use itertools.zip_longest instead (give
it a fillvalue of '').
2. The lines aren't all the same length, so you'll need to pad the
left-hand one to the correct length (20 characters) to make everything
line-up.
More information about the Python-list
mailing list