Alle Tage eines Jahres mit Python iterieren
English
Deutsch
Um alle Tage eines Jahres mit Python zu iterieren, kannst du dieses Funktions-Snippet verwenden:
all_dates_in_year.py
from collections import namedtuple
from calendar import monthrange
Date = namedtuple("Date", ["year", "month", "day"])
def all_dates_in_year(year=2019):
for month in range(1, 13): # Monat ist immer 1..12
for day in range(1, monthrange(year, month)[1] + 1):
yield Date(year, month, day)Anwendungsbeispiel:
all_dates_example.py
for date in all_dates_in_year(2019):
print(date)Dies wird ausgegeben (Auszug):
all_dates_output.txt
Date(year=2019, month=1, day=1)
Date(year=2019, month=1, day=2)
Date(year=2019, month=1, day=3)
[...]
Date(year=2019, month=2, day=27)
Date(year=2019, month=2, day=28)
Date(year=2019, month=3, day=1)
[...]
Date(year=2019, month=12, day=29)
Date(year=2019, month=12, day=30)
Date(year=2019, month=12, day=31)Du kannst auch meine Python-3-Bibliothek UliEngineering mit pip install -U UliEngineering installieren und dann all_dates_in_year() verwenden, indem du es wie folgt importierst:
all_dates_uliengineering.py
from UliEngineering.Utils.Date import *Siehe auch Anzahl der Tage eines Monats in Python ermitteln
Check out similar posts by category:
Python
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow