如何在Python 2.7.13脚本中将日志文件中的时间戳字符串存储到变量中?

时间:2019-04-18 14:22:02

标签: python

我正在尝试使用此正则表达式“ ^ \ d {4}-\ d {2}-\ d {2} \ w \ d {2}:\ d {2}:\ d在日志文件中查找时间戳{2},\ d {4}“,将它们存储在变量中并在条件语句中使用

我尝试使用re.search(),但它需要两个参数。并打印整行而不是时间戳。

file = os.listdir("/asp_tech/huwd/CASES/TECH-390_trade_adaptor_monitor")
no = len(file)
counter = 0
counter_log = 0
logs = []

#Accessing the files in the directory and filtering the ones we need
while counter < no:
    if "trade-adaptor_o91.log" in file[counter]:
        logs.append(file[counter])
    counter += 1
len_logs = len(logs)

#Accessing the relevant files one by one
while counter_log < len_logs:
    trial = "/asp_tech/huwd/CASES/TECH-390_trade_adaptor_monitor/" + logs[counter_log]
    timeLog = open(trial, "r")

    for line in timeLog:
        re.search("^\d{4}-\d{2}-\d{2}\w\d{2}:\d{2}:\d{2},\d{4}",line)
    counter_log += 1

I expect to save a timestamp e.g. 2019-04-05T10:10:16,841 into a variable

0 个答案:

没有答案