[英]Why am I getting this NameError: name "dt' is not defined
I'm new to Python and I'm trying to scrape reddit and convert the UNIX timestamp into a date.我是 Python 的新手,我正在尝试抓取 reddit 并将 UNIX 时间戳转换为日期。 However, when I run this code I'm getting a Name Error.
但是,当我运行此代码时,出现名称错误。 Can anyone tell me why I'm getting this error?
谁能告诉我为什么我会收到这个错误? Thanks.
谢谢。
from datetime import datetime
def get_date(created):
return dt.datetime.fromtimestamp(created)
_timestamp = topics_data["created"].apply(get_date)
dt
means nothing in your current code what the interpreter kindly tells you. dt
在您当前的代码中没有任何解释器告诉您的意思。
What you're trying to do is to call a datetime.datetime.fromtimestamp()
您要做的是调用
datetime.datetime.fromtimestamp()
You can change your import
to:您可以将
import
更改为:
import datetime as dt
and then dt
will be an alias for datetime
package so dt.datetime.fromtimestamp(created)
will work.然后
dt
将成为datetime
时间 package 的别名,因此dt.datetime.fromtimestamp(created)
将起作用。
But this is not the only solution.但这不是唯一的解决方案。 The below also will work:
以下也将起作用:
import datetime
datetime.datetime.fromtimestamp(created)
from datetime import datetime
datetime.fromtimestamp(created)
You can check this out to have a better understatement of the differences.您可以检查一下以更好地低估差异。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.