Python 读取log文本将13位时间戳转换为时间

转换前test.log

b99fc31add4b7c041","timestamp":"1519443717289"},"event":"1ed02e727dfa0

TimeConversion.py

import time
import re

path = 'D:\\work_sql\\text.log'  # 文件路径

with open(path, 'r', encoding='utf-8') as f:
    for each_line in f:     # 遍历文件的每一行
        reg = r'"timestamp":"(.{13})'
        word_reg = re.compile(reg)
        word_reg_list = re.findall(word_reg, each_line)
        for word in word_reg_list:
            # print(word)
            word = int(word)
            timeStamp = float(word / 1000)
            timeArray = time.localtime(timeStamp)
            otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
            # print(otherStyleTime)
            word = str(word)
            each_lines = each_line.replace(word, otherStyleTime)
            with open('TimeConversion.log', 'a', encoding='utf-8') as f1:  # 追加写入一个文件中
                f1.write(each_lines)

转换后test.log

b99fc31add4b7c041","timestamp":"2018-02-24 11:41:57"},"event":"1ed02e727dfa0

 

展开阅读全文

没有更多推荐了,返回首页