animal.py 1.24 KB
import chardet

def detect_encoding(file_path):
    with open(file_path, 'rb') as f:
        result = chardet.detect(f.read())
    return result['encoding']

def subLineStr(text):
    # 找到 "调用" 和 "时发生异常" 的位置
    start_index = text.find("调用") + len("调用")
    end_index = text.find("时发生异常")

    # 使用切片截取中间部分
    result = text[start_index:end_index]

    # print(result)  # 输出:stoassistant.api.score.detail
    return result


# 使用 chardet 检测编码格式
file_path = 'F:/users/tecent/qwechat/WXWork/1688853634415099/Cache/File/2023-07/rop-scrm-xxxxx.log'
file_encoding = detect_encoding(file_path)
print(f'文件编码格式:{file_encoding}')

methodMapper = {}
# 打开文件并读取内容,指定正确的编码格式
with open(file_path, 'r', encoding=file_encoding) as file:
    line = file.readline()
    while line:
        # 处理当前行的内容,例如打印
        # print(line.strip())  # 使用 strip() 方法去除行末尾的换行符
        line = file.readline()
        prifex = line[:4]
        if prifex=='2023':
            methodName = subLineStr(line)
            methodMapper[methodName] = 0

for mekey,meValue in methodMapper.items():
    print(f'method:{mekey}')