在此问题中,我通过按行拆分日志信息并将其追加到列表中来解析日志信息。然后,我通过提取时间戳并删除所有不是数字的内容来“清理”它。现在,我尝试将每个数字(仍然是字符串)转换为整数类型。
我仍然遇到错误,并且真的不知道该如何解决该问题。我添加了包含以下时间戳信息的网址:
url:http://projects.bobbelderbos.com/pcc/messages.log
import os
import urllib.request
SHUTDOWN_EVENT = 'Shutdown initiated'
# prep: read in the logfile
logfile = #deleted for security reasons
#print(logfile)
urllib.request.urlretrieve('http://projects.bobbelderbos.com/pcc/messages.log', logfile)
with open(logfile) as f:
loglines = f.readlines()
print(loglines)
def convert_to_datetime(line):
#takes in the log, separates by line and makes a list per line.
splitted_list = [i.split() for i in line]
time_stamp_list = [i[1] for i in splitted_list]
time_and_date = [i.split('T') for i in time_stamp_list]
test_parse = [
i.replace('-', ' ')
.replace("T", " ")
.replace(":", " ")
.split(" ") for i in time_stamp_list
]
print('test parse: ')
print(test_parse)
#This returns the necessary values into strings. Now it's time to convert them into ints, and then a dictionary
#this is where I am having trouble:
test_parse_num = [int(i) for i in test_parse]
print(test_parse_num)
答案 0 :(得分:1)
这两个解决方案需要起作用:
import os
import urllib.request
SHUTDOWN_EVENT = 'Shutdown initiated'
# prep: read in the logfile
logfile = #file that i have from the link
#deleted for security reasons
#print(logfile)
urllib.request.urlretrieve('http://projects.bobbelderbos.com/pcc/messages.log', logfile)
with open(logfile) as f:
loglines = f.readlines()
print(loglines)
def convert_to_datetime(line):
#takes in the log, separates by line and makes a list per line.
splitted_list = [i.split() for i in line]
time_stamp_list = [i[1] for i in splitted_list]
time_and_date = [i.split('T') for i in time_stamp_list]
test_parse = [
i.replace('-', ' ')
.replace("T", " ")
.replace(":", " ")
.split(" ") for i in time_stamp_list
]
print('test parse: ')
print(test_parse)
#This returns the necessary values into strings. Now it's time to convert them into ints, and then a dictionary
test_parse_num = []
#return one list with all the nums:
for k in test_parse:
for i in k:
test_parse_num.append(int(i))
print(test_parse_num)
或:
import os
import urllib.request
SHUTDOWN_EVENT = 'Shutdown initiated'
# prep: read in the logfile
logfile = #file that i have from the link
#deleted for security reasons
#print(logfile)
urllib.request.urlretrieve('http://projects.bobbelderbos.com/pcc/messages.log', logfile)
with open(logfile) as f:
loglines = f.readlines()
print(loglines)
def convert_to_datetime(line):
#takes in the log, separates by line and makes a list per line.
splitted_list = [i.split() for i in line]
time_stamp_list = [i[1] for i in splitted_list]
time_and_date = [i.split('T') for i in time_stamp_list]
test_parse = [
i.replace('-', ' ')
.replace("T", " ")
.replace(":", " ")
.split(" ") for i in time_stamp_list
]
print('test parse: ')
print(test_parse)
#This returns the necessary values into strings. Now it's time to convert them into ints, and then a dictionary
test_parse_num = []
#returns a list with lists in it(a list for every log):
for k in test_parse:
new = []
for i in k:
new.append(int(i))
test_parse_num.append(new)
print(test_parse_num)
答案 1 :(得分:0)
不确定这是否是您要的东西,但是您有:
>>> from datetime import datetime
>>> with open('messages.log', 'r') as f:
... lines = f.readlines()
>>> for line in lines:
... pieces = line.split(' ')
... date_format = r'%Y-%m-%dT%H:%M:%S'
... date = datetime.strptime(pieces[1], date_format)
... print(date)
...
2014-07-03 23:24:31
2014-07-03 23:24:31
2014-07-03 23:24:31
2014-07-03 23:24:31