我有下面的代码写使用Pandas库:
# function : check for time
def checktime(reportedtime, scheduledtimestart, scheduledtimeend):
if scheduledtimestart <= reportedtime <= scheduledtimeend:
return "Yes"
else:
return "No"
df.loc[sub_df.index, 'Access Valid'] = checktime(time_date, schedule_start_date, schedule_end_date)
df.to_excel(current_directory + '/files/updated_report.xlsx', index=False)
df
是主/原始 Dataframe 。而sub_df
是修改/过滤后的 Dataframe 。Access Valid
是我试图向其附加值的列。
该程序检查用户time_date
发出请求的时间是否在其schedule_start_date
和schedule_end_date
之间。然后,它将列Access Valid
下的单元格更新为yes
或no
。
问题
我被要求修改我的代码,并使用import csv
库,而不是import pandas
,由于大小,但我无法确定指定行的位置,以便将Access Valid
附加到csv文件并将其导出为excel文件。
用户列表:
user; scheduled start date; scheduled end date;
test1; 2022-09-01 00:00:00; 2022-09-30 23:59:59;
test2; 2022-09-01 00:00:00; 2022-09-14 23:59:59;
示例文件结构(文件包含超过1000个请求):
time; user; Access Valid
2022-09-20 09:09:00; test1;
2022-09-21 05:00:00; test2;
所需的文件输出:
time; user; Access Valid
2022-09-20 09:09:00; test1; yes
2022-09-21 05:00:00; test2; no
1条答案
按热度按时间7ivaypg91#
使用
csv
,代码将如下所示: