查找键为时间戳的Python字典值

2024-03-29 14:39:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本字典,里面有日期和时间戳。如果我用以下内容查找钥匙:

print(ActiveByInterval.keys())

这是我的输出:

dict_keys([Timestamp('2017-07-01 00:00:00'), Timestamp('2018-07-01 00:00:00'), Timestamp('2019-07-01 00:00:00')])

但是,如果我试图通过这些键中的一个来获取项目,就会收到一个错误。我尝试了以下方法:

print(ActiveByInterval[Timestamp('2017-07-01 00:00:00')])
error:  name 'Timestamp' is not defined

print(ActiveByInterval['Timestamp('2017-07-01 00:00:00')'])
error:  invalid syntax

print(ActiveByInterval[('2017-07-01 00:00:00')])
error:  invalid syntax

print(ActiveByInterval['2017-07-01 00:00:00'])
error:  keyerror

print(ActiveByInterval[2017-07-01 00:00:00])
error:  invaluid token

如何通过这些键之一调用值?你知道吗


Tags: 项目方法name字典错误时间errorkeys
2条回答

打印(ActiveByInterval)[pd.时间戳('2017-07-01 00:00:00')])工作。你知道吗

使用-

from pandas import Timestamp

相关问题 更多 >