Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取日期和时间_Python_Python 3.x - Fatal编程技术网

Python 如何获取日期和时间

Python 如何获取日期和时间,python,python-3.x,Python,Python 3.x,我下面有一根绳子 Str= """Feb 11 22 error Start" 我需要检查上面的字符串中是否有错误,然后打印“是” 我是python新手。plz帮助。您可以使用此功能检查是否存在错误: def checkError(Str): if 'ERROR' in Str: openBracket = Str.find('(')+1 closedBracket = Str.find(')') status = "".join(Str

我下面有一根绳子

Str= """Feb 11 22 error Start"
我需要检查上面的字符串中是否有错误,然后打印“是”


我是python新手。plz帮助。

您可以使用此功能检查是否存在错误:

def checkError(Str):
    if 'ERROR' in Str:
        openBracket = Str.find('(')+1
        closedBracket = Str.find(')')
        status = "".join(Str[openBracket:closedBracket])
        error = "\n".join(['YES',", ".join(Str.split('\n\n')[2].split(', ')[1:]),status])
        return error
    else:
        return 'No errors found.'

error = checkError(Str)
print(error)
输出:

YES
22:17:31 Wed Feb 11, 2015
software ERROR

我包括了两种不同的方法。regex和find从给定字符串中获取所需元素。一个人可以使用任何方法

import re

def get_log(Str):
    if 'ERROR' in Str:
        date = re.search('\d{2}:\d{2}:\d{2} \w{3} \w{3} \d{2}, \d{4}', Str)
        error = Str[Str.find("(")+1:Str.find(")")]
        result = 'YES,\n'+date.group()+',\n'+error
        print(result)

get_log(Str)

日期=,.joinStr.split','[1:]基于有限的信息“”。joinStr.split'[-5:]@VasilisG。我需要将字符串限制在2015年2月11日星期三22:17:31以下这一行。我该怎么做?你能用你想要的输出更新你的问题来澄清这一点吗?更新了它..请尝试在你的代码答案中添加一些注释,解释为什么它可能有助于OP。