【发布时间】:2020-11-24 19:00:49
【问题描述】:
我试图通过从日期中提取月份来获取本月工作的所有员工的输出,但我收到此错误:
month = int(row[1].split('-')[1])
IndexError: list index out of range
考勤日志 csv 中的一行如下所示:
"404555403","2020-10-14 23:58:15.668520","Chandler Bing"
我不明白为什么它超出了范围? 感谢您的帮助!
import csv
import datetime
def monthly_attendance_report():
"""
The function prints the attendance data of all employees from this month.
"""
this_month = datetime.datetime.now().month
with open('attendance_log.csv', 'r') as csvfile:
content = csv.reader(csvfile, delimiter=',')
for row in content:
month = int(row[1].split('-')[1])
if month == this_month:
return row
monthly_attendance_report()
【问题讨论】:
-
您的逻辑适用于列表;尝试检查您是否正确地遍历 CSV 文件(即尝试打印出
row)。