简体   繁体   English

Python-如何在不使用日历,日期时间的情况下获取当前工作日

[英]Python - how to get the current weekday without using calendar, datetime

I'm having a little bit of a problem here. 我在这里有点问题。 My system lets me 我的系统让我

pip install calendar 点安装日历

but for some reason it doesn't recognize it when importing it in my script. 但由于某种原因,在我的脚本中将其导入时无法识别它。

Whereas for datetime , the module datetime.datetime gives me an error saying that 而对于datetime ,模块datetime.datetime给我一个错误,说

there is no attribute datetime in datetime. datetime中没有属性datetime。

And datetime.date wants me to feed it a datetime.datetime object, therefore it doesn't let me input strings, lists, ints, or whatever. 而且datetime.date希望我提供给它一个datetime.datetime对象,因此它不允许我输入字符串,列表,整数或任何其他内容。

Does anyone know another possible way to get the current weekday , or how to fix one of the above ? 有谁知道获取当前工作日的另一种可能方法 ,或者如何解决上述问题之一

Thanks 谢谢

using time . time

from time import time, localtime, strftime

x=localtime(time())
strftime("%A",x) # 'Tuesday'

It looks like your most recent errors are caused by the same thing causing the original error with the datetime module. 看来您最近的错误是由引起datetime模块原始错误的同一件事引起的。

there is no attribute datetime in datetime. datetime中没有属性datetime。

Is likely the same as the error caused by this line hoursMinsSecs = time.strftime('%H, %M, %S') 可能与此行hoursMinsSecs = time.strftime('%H, %M, %S')引起的错误相同

These errors are caused because both the datetime and time modules have objects with the same name as the module. 引起这些错误的原因是datetime和time模块都具有与模块同名的对象。 (time.time and datetime.datetime). (time.time和datetime.datetime)。 Therefore, how you import the modules or objects becomes very important. 因此, 如何导入模块或对象变得非常重要。

In the time example, your import statement is: time示例中,您的导入语句为:

from time import time, localtime, strftime

As a result, the time object has been imported into your namespace and not the time module . 结果,时间对象已导入到您的名称空间中,而不是时间模块中 Therefore, when you try to run time.strftime('%H, %M, %S') you get an error that strftime isn't an attribute of the time object. 因此,当您尝试运行time.strftime('%H, %M, %S')会收到错误消息,指出strftime不是时间对象的属性。

Instead, since you already imported strftime, just call that directly and do not qualify it with time. 相反,由于您已经导入了strftime,因此只需直接调用它,而不用时间限制它。 (ie strftime('%H, %M, %S') ) (即strftime('%H, %M, %S')

For your datetime example, you probably had the import statement from datetime import datetime , but somewhere in your code used datetime.datetime 对于您的datetime示例,您可能具有from datetime import datetime的import语句,但是代码中的某处使用了datetime.datetime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM