我有一个以下格式的文件。在
15/07/2010 14:14:13 changed_status_from_Offline_to_Available
15/07/2010 15:01:09 changed_status_from_Available_to_Offline
15/07/2010 15:15:35 changed_status_from_Offline_to_Away became_idle
15/07/2010 15:16:29 changed_status_from_Away_to_Available became_unidle
15/07/2010 15:45:40 changed_status_from_Available_to_Away became_idle
15/07/2010 16:05:40 changed_status_from_Away_to_Available became_unidle
15/07/2010 16:51:39 changed_status_from_Available_to_Offline
20/07/2010 13:07:26 changed_status_from_Offline_to_Available
我需要用python创建一个函数,它必须有参数:日期和时间。如果日期匹配并且时间小于函数调用中的时间,则它应该读取文件并返回第二个状态。那就是
假设我调用函数returnstatus(15/07/2010,15:10:01)。 该函数应该转到该文件并返回用户在该日的状态,在本例中为“脱机”。在
我是一个Python新手,任何帮助都将非常感谢。在
我建议您在python文档中有一个read,特别是time module和函数strptime,它们可以将时间的文本表示解析为编程表示。在
以您在问题中编写的方式调用
returnstatus
肯定会失败,您可能需要使用时间的字符串表示(即“15/07/2010 15:10:01”)或通过传递时间模块中定义的一种数据类型来调用它。在编辑:显然,如果传入一个字符串时间,那么在文件中查找它就容易得多:
这有道理吗?或者你想让我解释一下吗?在
编辑
你刚才说的是真的吗?在
换句话说,如果调用
return_status( 16/07/2010, <some.time> )
,会发生什么?你应该下线吗?在另一个编辑
我对它进行了编辑,以便进行合理的
datetime
比较。我想你已经错误地理解了这个不等式:我们循环遍历文件中的行,直到我们希望获取的日期之后的第一行。一旦这个测试失败,line
是所需日期之后的第一行,因此它的from
值就是我们请求时的状态。您应该使用datetime.datetime
调用函数。在基本上,您需要做的是从日志中提取日期和时间,并将其转换为易于比较的格式。输入
datetime
。在相关问题 更多 >
编程相关推荐