简体   繁体   English

Python日期时间1970-01-01 07:00:07

[英]Python Datetime 1970-01-01 07:00:07

I have code snippet like this: 我有这样的代码片段:

import re 
from datetime import datetime as dt 
from pprint import pprint as pp

__lin_regex = "(.*)msg='(.*)'"

events = [
    "type=USER_AUTH msg=audit(1565448223.294:46713): pid=4499 uid=0 auid=4294967295 ses=4294967295 msg='op=PAM:authentication op-test acct=\"root\" exe=\"/usr/sbin/sshd\" hostname=49.88.112.75 addr=49.88.112.75 terminal=ssh res=failed'"
]

logs = {}

for event in events:
    vm_info = ['name', 'id']

    results = ''.join(list(re.match(__lin_regex, event).groups()))

    split_results = results.split(' ')

    split_results_length = len(split_results)
    for i in range(0, split_results_length-1):
        if split_results[i].find('=') == -1:
            split_results[i - 1] = split_results[i - 1] + ' ' +  split_results[i]
            split_results.remove(split_results[i])

    pid = split_results.pop(2).split('=')[1]

    if pid not in logs:
        logs[pid] = vm_info + [pid] + [item.split('=')[1] for item in split_results]
    logs[pid][4] = dt.fromtimestamp(int(float(re.findall("([0-9.]+):", logs[pid][4])[0]))).strftime("%Y-%m-%d %H:%M:%S")

pp(logs)

But when I have large amount of events, their output it 1970-01-01 07:00:07, when I try to print 1000 data still works, but when I print it all it didn't work, any idea? 但是,当我有大量事件时,它们的输出是1970-01-01 07:00:07,当我尝试打印1000个数据时仍然有效,但是当我将其打印时,所有操作均无效,您知道吗?

Had a look at your code, and for the sake of it, replaced / added the following lines, which let me do 10,000 events with the same timestamp but different pid's : 看了一下您的代码,为了方便起见,替换/添加了以下几行,这些行使我可以使用相同的时间戳记但使用不同的pid's来执行10,000个事件:

events = [
    "type=USER_AUTH msg=audit(1565448223.294:46713): pid=4499 uid=0 auid=4294967295 ses=4294967295 msg='op=PAM:authentication op-test acct=\"root\" exe=\"/usr/sbin/sshd\" hostname=49.88.112.75 addr=49.88.112.75 terminal=ssh res=failed'"
] * 10000

for i, event in enumerate(events):
    events[i] = event.replace("pid=4499", "pid="+str(4499+i))

The date string output for all 10,000 events was correct, so I think the issue would likely have to do with (a) your regex, or (b) your input data. 所有10,000个事件的日期字符串输出都是正确的,因此我认为问题可能与(a)您的正则表达式或(b)您的输入数据有关。

The way I would debug it, if I were you, is to just use pdb , and look for the example output you're seeing, eg: 如果是您,我调试它的方法是只使用pdb ,然后查找您看到的示例输出,例如:

import pdb

... rest of your code ...

    logs[pid][4] = dt.fromtimestamp(int(float(re.findall("([0-9.]+):", logs[pid][4])[0]))).strftime("%Y-%m-%d %H:%M:%S")
    if logs[pid][4] == "1970-01-01 07:00:07":
        pdb.set_trace()

At that point, in the interactive debugger you can have a look at the variables involved & see what your input event string is that's causing the problem (chances are, the data isn't as well-formatted as you expect it to be). 到那时,您可以在交互式调试器中查看所涉及的变量并查看导致问题的输入event字符串什么(可能是,数据的格式不如您期望的那样)。 If it's not obvious, you may be able to tell what's going on by just stepping through the code using n & inspecting variable assignments. 如果不是很明显,您可以通过使用n逐步检查代码并检查变量分配来判断发生了什么。

You can use !import code; code.interact(local=vars()) 您可以使用!import code; code.interact(local=vars()) !import code; code.interact(local=vars()) in the debugger to bring the local variables into a normal python shell if you're having trouble with name clashes due to debugger commands. 如果由于调试器命令而导致名称冲突时遇到问题,请在调试器中使用!import code; code.interact(local=vars())将本地变量带入普通的python shell中。

暂无
暂无

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

相关问题 Python 读取日期时间字符串(使用 python 读取 T01:00:00-07:00) - Python reading datetime string ( reading T01:00:00-07:00 with python ) 在Python中文件创建日期显示为1970-01-01 00:00:00 - File creation date is showing as 1970-01-01 00:00:00 in Python Python-所有电子邮件的日期为1970-01-01 01:00 - Python - All emails dated from 1970-01-01 01:00 对于 1970-01-01 之前的日期,Windows 上的 datetime timestamp() 的解决方法 - Workaround for datetime timestamp() on Windows for dates preceeding 1970-01-01 如何从 python 中的日期时间获取时间 2020-07-25 | 03:01 - How to get time from datetime in python 2020-07-25 | 03:01 如何在Python 3中将字符串['2019-06-01T23:07:02.000 + 0000']转换为datetime对象 - How to convert the string['2019-06-01T23:07:02.000+0000'] to datetime object in Python 3 如何在 python 中将此字符串 '2022-07-07T10:30:00-07:00' 转换为 datetime obj? - How would I convert this string '2022-07-07T10:30:00-07:00' into datetime obj in python? Python 2.7 - 使用 +01:00 转换日期时间 - Python 2.7 - Convert datetime with +01:00 为什么对两个十六进制字符串进行异或运算会产生以下格式的输出:'\\ x02 \\ x07 \\ x01P \\ x00 \\ x07 - Why XORing two hex strings produce an output in the following format: '\x02\x07\x01P\x00\x07 如何在 python 中将这种格式 '2020-01-01 00:00:00+00:00' 的 str 转换为日期时间? - How to convert a str like this format '2020-01-01 00:00:00+00:00' to datetime in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM