您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何在Python中解析此自定义日志文件

如何在Python中解析此自定义日志文件

使用@Joran Beasley的答案,我想出了以下解决方案,它似乎有效:

def generateDicts(log_fh):
    currentDict = {}
    for line in log_fh:
        if line.startswith(matchDate(line)):
            if currentDict:
                yield currentDict
            currentDict = {"date":line.split("__")[0][:19],"type":line.split("-",5)[3],"text":line.split("-",5)[-1]}
        else:
            currentDict["text"] += line
    yield currentDict

with open("/Users/stevenlevey/Documents/out_folder/out_loyalty@R_450_2419@/log_CardsReport_20150522164636.logs") as f:
    listNew= list(generateDicts(f))
    def matchDate(line):
        matchThis = ""
        matched = re.match(r'\d\d\d\d-\d\d-\d\d\ \d\d:\d\d:\d\d',line)
        if matched:
            #matches a date and adds it to matchThis            
            matchThis = matched.group() 
        else:
            matchThis = "NONE"
        return matchThis
python 2022/1/1 18:33:57 有214人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶