import re import ast import datetime import os import constant from log_module.log import logger_debug, logger_common from utils import huaxin_util # 获取logger实例 logger = logger_common # 读取log日志并转换格式 def read_and_parse_log(log_file_path): # 打开并读取日志文件 with open(log_file_path, 'r', encoding='utf-8') as log_file: lines = log_file.readlines() # print(f"lines=={lines}") # 初始化一个列表数据 来安置转换好的日志文件 parsed_logs = [] # 正则表达式匹配日志格式,例如:YYYY-MM-DD HH:MM:SS LEVEL PID TID MESSAGE # 这里我们假设一个简化的格式:HH:MM:SS LEVEL MESSAGE # 正则表达式匹配日志格式,例如:YYYY-MM-DD HH:MM:SS.mmm | LEVEL | MODULE:FUNCTION:LINE - MESSAGE log_regex = re.compile( r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3} \|\s*(\w+)\s*\|\s*([\w.]+:\w+:\d+) -\s*(.*)$') for line in lines: match = log_regex.match(line.strip()) if match: # 提取时间戳、日志级别和日志信息 timestamp, level, message = match.groups() # 这里我们不添加时间戳和日志级别到parsed_logs,只添加日志信息 parsed_logs.append(message) return parsed_logs # 找到具体目标个股L2中有无大单记录 def find_L2_big_order_of_code(code): # 获取当前日期,并格式化为 'YYYY-MM-DD' current_date = datetime.datetime.now().strftime('%Y-%m-%d') # 声明要转义的文件的路径 log_file_path = f'{constant.L2_LOG_PATH}/transaction.{current_date}.log' # 检查文件是否存在,如果存在则继续处理,如果不存在则就此打住 if not os.path.exists(log_file_path): logger.info(f"Error错误:{log_file_path}----文件不存在!") return parsed_logs = read_and_parse_log(log_file_path) # 使用列表推导式和 ast.literal_eval() 将字符串转换为列表 true_lists = [ast.literal_eval(log) for log in parsed_logs] # 初始化一个标志变量,用于检查是否找到了特定股票代码 found = False # 遍历转换后的列表 for sublist in true_lists: if sublist and sublist[0] and sublist[0][0] == code: # print(f"找到大单的对应行列表 sublist[0]===={sublist[0]}") # 格式 # sublist[0]====['002413', 0, [3996643, 287200, 1421640, 93453070, 4.95]] # sublist[0]====['股票代码', 买入, [订单号, 手数, 订单金额, 时间, 成交价]] # 时间是华鑫格式要转换一下 if sublist[0][2][3] is not None: huaxin_time_format = sublist[0][2][3] order_transaction_time = huaxin_util.convert_time(huaxin_time_format) logger.info(f"找到L2大单,对应行列表: sublist[0]===={sublist[0]} 大单金额:{sublist[0][2][2]}元 ,转换后时间格式:{order_transaction_time}") found = True # 设置标志为 True,表示找到了 # 检查是否没有找到特定股票代码 if not found: logger.info(f"没有找到L2大单:{code}") # print(f"没有找到这个货:{code}") return found if __name__ == '__main__': # 测试的时候可以调用一下 find_L2_big_order_of_code('002384')