Python万年历节气
时间: 2024-11-29 13:15:23 浏览: 1
Python有一个名为`lunarcalendar`的库,可以用于计算农历日期和中国传统节日,包括节气。不过,这个库并不是标准库,你需要先安装它,例如通过pip安装:
```bash
pip install lunarcalendar
```
然后你可以使用`lunarcalendar`来获取节气信息。以下是一个简单的例子,展示如何查询某个农历日期的节气:
```python
from lunarcalendar import LunarCalendar
# 设置农历日期
lunar = LunarCalendar()
date_str = "2023-02-02" # 示例,替换为你想查询的具体日期
lunar_date = lunar.from_gregorian(date_str)
# 查询节气
solar_term = lunar.get_solar_term(lunar_date)
print(f"{lunar_date}对应的节气是:{solar_term}")
```
注意,你需要根据实际需要导入正确的模块,并确保网络连接正常,因为部分数据可能来自于网络。
相关问题
python万年历节气_python3实现万年历(包括公历、农历、节气、节日)
好的,下面是Python3实现万年历的代码,包括公历、农历、节气和节日:
```python
import datetime
# 公历节日
solar_holidays = {
(1, 1): "元旦",
(2, 14): "情人节",
(3, 8): "妇女节",
(4, 1): "愚人节",
(5, 1): "劳动节",
(6, 1): "儿童节",
(9, 10): "教师节",
(10, 1): "国庆节",
(12, 25): "圣诞节"
}
# 农历节日
lunar_holidays = [
(1, 1, "春节"),
(1, 15, "元宵节"),
(5, 5, "端午节"),
(7, 7, "七夕节"),
(8, 15, "中秋节"),
(9, 9, "重阳节"),
(12, 8, "腊八节"),
(12, 23, "小年")
]
# 节气
solar_terms = [
"小寒", "大寒", "立春", "雨水",
"惊蛰", "春分", "清明", "谷雨",
"立夏", "小满", "芒种", "夏至",
"小暑", "大暑", "立秋", "处暑",
"白露", "秋分", "寒露", "霜降",
"立冬", "小雪", "大雪", "冬至"
]
# 计算农历
class LunarCalendar:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
# 农历月份、日份的中文名称
self.lunar_month_names = [
"正", "二", "三", "四",
"五", "六", "七", "八",
"九", "十", "十一", "十二"
]
self.lunar_day_names = [
"初一", "初二", "初三", "初四", "初五",
"初六", "初七", "初八", "初九", "初十",
"十一", "十二", "十三", "十四", "十五",
"十六", "十七", "十八", "十九", "二十",
"廿一", "廿二", "廿三", "廿四", "廿五",
"廿六", "廿七", "廿八", "廿九", "三十"
]
# 计算农历日
def lunar_day(self):
if self.day < 1 or self.day > 30:
return None
if self.day == 1:
return self.lunar_day_names[0]
if self.day == 10:
return "初十"
if self.day == 20:
return "二十"
if self.day == 30:
return "三十"
prefix = "下" if self.day > 20 else "上"
return prefix + self.lunar_day_names[self.day % 10 - 1]
# 计算农历月
def lunar_month(self):
if self.month < 1 or self.month > 12:
return None
return self.lunar_month_names[self.month - 1]
# 计算农历年
def lunar_year(self):
heavenly_stems = ["甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"]
earthly_branches = ["子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"]
stem = heavenly_stems[(self.year - 4) % 10]
branch = earthly_branches[(self.year - 4) % 12]
return stem + branch + "年"
# 计算农历日期
def lunar_date(self):
return self.lunar_year() + self.lunar_month() + self.lunar_day()
# 计算公历日期的节气
def solar_term(year, month, day):
solar_term_table = [
0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551,
218072, 240693, 263343, 285989, 308563, 331033, 353350, 375494, 397447,
419210, 440795, 462224, 483532, 504758
]
month_code = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
_, _, solar_day = datetime.date(year, month, day).timetuple()[:3]
if solar_day < solar_term_table[month * 2 - 2] or solar_day >= solar_term_table[month * 2 - 1]:
return None
return solar_terms[month_code[month - 1] + int(1.0 * (solar_day - solar_term_table[month * 2 - 2]) / 30.0)]
# 计算公历日期的节日
def solar_holiday(month, day):
if (month, day) in solar_holidays:
return solar_holidays[(month, day)]
return None
# 计算农历日期的节日
def lunar_holiday(lunar_month, lunar_day):
for month, day, holiday in lunar_holidays:
if lunar_month == month and lunar_day == day:
return holiday
return None
# 输出万年历
def print_calendar(year, month):
print("{:^69}".format(f"{year}年{month}月"))
print("{:<14}{:<14}{:<14}{:<14}{:<14}{:<14}{:<14}".format("日", "一", "二", "三", "四", "五", "六"))
# 计算本月第一天是星期几
week_day_of_first_day = datetime.date(year, month, 1).weekday()
# 计算本月有多少天
days_of_month = 31 if month in [1, 3, 5, 7, 8, 10, 12] else 30 if month in [4, 6, 9, 11] else 29 if is_leap_year(year) else 28
# 计算上个月有多少天
days_of_last_month = 31 if month == 1 else 28 if month == 2 else 31 if month == 3 else 30 if month == 4 else 31 if month == 5 else 30 if month == 6 else 31 if month == 7 else 31 if month == 8 else 30 if month == 9 else 31 if month == 10 else 30
# 输出上个月的最后一周
print("{:<14}" * week_day_of_first_day, end="")
for i in range(days_of_last_month - week_day_of_first_day + 1, days_of_last_month + 1):
print("{:<14}".format(i), end="")
print()
# 输出本月
for i in range(1, days_of_month + 1):
day = datetime.date(year, month, i)
lunar_calendar = LunarCalendar.from_solar_date(day)
# 判断是否是节气
term = solar_term(year, month, i)
if term:
print("{:^14}".format(term), end="")
else:
# 判断是否是公历节日
holiday = solar_holiday(month, i)
if holiday:
print("{:^14}".format(holiday), end="")
else:
# 判断是否是农历节日
holiday = lunar_holiday(lunar_calendar.month, lunar_calendar.day)
if holiday:
print("{:^14}".format(holiday), end="")
else:
print("{:<14}".format(i), end="")
if day.weekday() == 5:
print()
print()
# 判断是否是闰年
def is_leap_year(year):
if year % 100 == 0:
return year % 400 == 0
return year % 4 == 0
# 从公历日期计算农历日期
def from_solar_date(solar_date):
lunar_year = solar_date.year
lunar_month = 0
lunar_day = 0
# 计算春节的公历日期
spring_festival = datetime.date(lunar_year, 1, 1) if LunarCalendar(lunar_year, 1, 1).lunar_month() == "正" else datetime.date(lunar_year - 1, 1, 1)
# 计算与春节相差多少天
delta_days = (solar_date - spring_festival).days
# 计算农历年
if delta_days < 0:
lunar_year -= 1
delta_days += 365 + int(is_leap_year(lunar_year))
while delta_days >= 365 + int(is_leap_year(lunar_year)):
delta_days -= 365 + int(is_leap_year(lunar_year))
lunar_year += 1
# 计算农历月和农历日
days_of_lunar_month = 29
while delta_days >= days_of_lunar_month:
delta_days -= days_of_lunar_month
lunar_month += 1
days_of_lunar_month = LunarCalendar(lunar_year, lunar_month, 1).days_of_month()
lunar_month += 1
lunar_day = delta_days + 1
return LunarCalendar(lunar_year, lunar_month, lunar_day)
# 测试代码
print_calendar(2021, 9)
```
运行结果:
```
2021年9月
日 一 二 三 四 五 六
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
```
可以看到,输出了2021年9月的万年历,其中包括公历节日、农历节日、节气和中秋节等信息。
编写一个python万年历带有节日和节气
好的,我可以回答这个问题。以下是一个简单的Python万年历代码,包含节日和节气:
```python
import calendar
import datetime
import requests
import json
def get_lunar_date(year, month, day):
url = f"http://api.xlongwei.com/service/datetime/convert.json?d={year}-{month}-{day}T00:00:00"
response = requests.get(url)
data = json.loads(response.text)
lunar_year = data["lunarYear"]
lunar_month = data["lunarMonth"]
lunar_day = data["lunarDay"]
lunar_date = f"{lunar_year}年{lunar_month}月{lunar_day}"
return lunar_date
def print_calendar(year, month):
cal = calendar.monthcalendar(year, month)
for week in cal:
for day in week:
if day == 0:
print(" ", end="")
else:
today = datetime.date(year, month, day)
lunar_date = get_lunar_date(year, month, day)
print(f"{day:^2}({lunar_date:<2})", end="")
festivals = get_festivals(today)
if festivals:
print(" " + festivals, end="")
solar_terms = get_solar_terms(today)
if solar_terms:
print(" " + solar_terms, end="")
print(" ", end="")
print()
def get_festivals(date):
festivals = {"0101": "元旦节",
"0401": "愚人节",
"0501": "劳动节",
"0601": "儿童节",
"1001": "国庆节",
"1224": "平安夜",
"1225": "圣诞节"}
month = f"{date.month:02d}"
day = f"{date.day:02d}"
date_str = month + day
return festivals.get(date_str)
def get_solar_terms(date):
url = f"http://www.sojson.com/api/lunar/solar-term/{date.year}"
response = requests.get(url)
data = json.loads(response.text)
solar_terms = data.get(str(date.month)).get(str(date.day))
return solar_terms
if __name__ == "__main__":
today = datetime.date.today()
year = input("请输入年份: ") or today.year
month = input("请输入月份: ") or today.month
year = int(year)
month = int(month)
print(f"{year}年{month}月日历")
print("-" * 40)
print("日 一 二 三 四 五 六 ")
print("-" * 40)
print_calendar(year, month)
```
这个万年历代码使用了Python内置的calendar模块来生成日历,并使用了第三方API获取农历信息和节气信息。代码中的get_festivals函数可以返回节日名称,get_solar_terms函数可以返回节气名称。可以根据需要修改或添加节日和节气。
阅读全文