[英]getting the attributeerror:__enter__ error
好的,我得到属性错误:输入语句。 试图让python检查下周一的日期,如果有该日期向前移动,那么如果将其更改为星期二,那么如果没有打印出错误:下周没有开始......但是虽然得到了那个错误并且不能想办法。
import os
import pandas as pd
import datetime as DT
from dateutil.relativedelta import relativedelta, MO, TU
hr_file = "Upcoming Hires.xlsx"
with pd.read_excel(hr_file, sheet_name="New Hires",parse_dates=["Start Date"]) as reader:
now = now = DT.datetime.now()
nm = now - relativedelta(weekday=MO(+1))
next_monday = now - relativedelta(weekday=MO(+1))
if next_monday in reader:
print("true")
if not next_monday in reader:
nm = now - relativedelta(weekday=TU(+2))
next_monday = nm.strftime("%#m/%#d/%#y")
if next_monday in reader:
print("had to move the date for the start date to tuesday but it True")
if not next_monday in reader:
print("error: there is no starter next week...")
得到这个错误
AttributeError Traceback (most recent call last)
c:\masterscript\test.py in <cell line: 1>()
----> 7 with pd.read_excel(hr_file, sheet_name="New Hires",parse_dates=["Start Date"]) as reader:
8 now = now = DT.datetime.now()
9 nm = now - relativedelta(weekday=MO(+1))
AttributeError: __enter__
您不需要上下文管理器来使用pd.read_excel
,因为此函数从文档返回 DataFrame 或 DataFrame 的字典:
返回: DataFrame 或 DataFrames 的字典
来自传入的 Excel 文件的 DataFrame。 有关何时返回 DataFrames 的字典的更多信息,请参阅 sheet_name 参数中的注释。
import pandas as pd
import numpy as np
with pd.DataFrame(np.random.rand(5,10)) as reader:
print(reader)
AttributeError Traceback (most recent call last)
----> 1 with pd.DataFrame(np.random.rand(5,10)) as reader:
2 print(reader)
AttributeError: __enter__
您可以将pd.read_excel
的返回值分配给一个变量,并将reader
调整到包含您需要的日期的列。 类似于以下内容:
...
hr_file = "Upcoming Hires.xlsx"
df = pd.read_excel(hr_file, sheet_name="New Hires",parse_dates=["Start Date"])
now = now = DT.datetime.now()
nm = now - relativedelta(weekday=MO(+1))
next_monday = now - relativedelta(weekday=MO(+1))
if next_monday in df['Start Date']:
print("true")
if not next_monday in df['Start Date']:
...
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.