python批量把时间戳批量变成日期时间
tformat.py#!/bin/pythonimport timeimport sysdata = sys.stdin.readlines()for line in data:seconds = int(line.split(':')[0])s_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(seconds))print s_tim
tformat.py
#!/usr/bin/python
import time
import sys
import re
def tf(line):
timestemp = re.findall(r'(16\d{8})\D', line)
for x in timestemp:
s_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(int(x)))
line = line.replace(x,s_time)
#s_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(seconds))
print line.rstrip()
return
if len(sys.argv)==1 :
data = sys.stdin.readlines()
for line in data:
tf(line)
else :
fp = open(sys.argv[1], "r")
while True:
line = fp.readline()
if not line:
break
tf(line)
使用方法:
chmod a+x ./tformat.py
cat file | ./tformat.py
或者
tformat.py file
另一种用法:
leon@ubuntu:~/work$ cat <<EOF | ./line.py
1646481238: Client 357734500178325 has exceeded timeout, disconnecting.
1646481238: Socket error on client 357734500178325, disconnecting.
1646481545: New client connected from 223.73.212.49 as 357734500178325 (c1, k600).
EOF
2022-03-05 11:53:58 Client 357734500178325 has exceeded timeout, disconnecting.
2022-03-05 11:53:58 Socket error on client 357734500178325, disconnecting.
2022-03-05 11:59:05 New client connected from 223.73.212.49 as 357734500178325 (c1, k600).

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。
更多推荐
所有评论(0)