我的代码如下,但是最后结果无法正确计算出课时,比如c-210这个教室,单独打印周次是6,但是节次只有32,最后相乘算完打印又得48,真不知道是哪里出问题了
import pandas as pd
import re
# 读取表格数据
df = pd.read_excel("C:\\Users\\15822\Desktop\\学校课表.xlsx", header=3)
# 输入教室名称
classroom = input("请输入教室名称:")
# 筛选指定教室的课程数据
classroom_data = df[df.iloc[:, 10] == classroom]
if classroom_data.empty:
print(f"未找到教室 {classroom} 的课程安排。")
else:
total_hours = 0
# 遍历每一行数据
for _, row in classroom_data.iterrows():
# 解析周次
weeks_str = row[8] # 周次列索引为8
weeks = 0
if isinstance(weeks_str, str):
# 匹配形如 "4-7" 或 "1,3,5-8" 的模式
week_ranges = re.findall(r'(\d+)-(\d+)|(\d+)', weeks_str)
for w in week_ranges:
if w[0] and w[1]: # 匹配到范围
start = int(w[0])
end = int(w[1])
weeks += end - start + 1
elif w[2]: # 匹配到单个数字
weeks += 1
# 解析节次
periods_str = row[9] # 节次列索引为9
periods = 0
if isinstance(periods_str, str):
# 匹配形如 "周一 1-2 节" 或 "周一 3-4 节 周二 5-6 节" 的模式
period_matches = re.findall(r'(?:周一|周二|周三|周四|周五|周六|周日) (\d+)-(\d+) ', periods_str)
for match in period_matches:
start = int(match[0])
end = int(match[1])
periods += end - start + 1
# 计算课时数并累加
if weeks > 0 and periods > 0:
total_hours += weeks * periods
print(f"教室 {classroom} 在本学期的总课时数为:{total_hours}")



