You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oracle AWR报告(.text/.lst/.html)转CSV及Python读取方法咨询

Oracle AWR报告处理问题解答

1. 解析.text/.lst为CSV及Python读取方法

可以将.text和.lst格式的AWR报告解析为CSV,这类文件本质是结构化纯文本,核心是定位报告中的表格区域,提取表头和数据行后写入CSV。

Python实现思路与示例代码:

  1. 先分析目标表格特征:AWR文本报告的表格通常以=====或类似分隔线开头/结尾,表头和数据行按固定列对齐。
  2. 读取文件后,通过字符串匹配或正则定位目标表格。
  3. 提取表头和数据,处理空格分隔的列(注意列值可能含空格,需按固定宽度拆分)。

示例代码(以解析"Top 5 Timed Foreground Events"表格为例):

import csv
import re

def parse_awr_text_to_csv(input_file, output_csv):
    with open(input_file, 'r', encoding='utf-8', errors='ignore') as f:
        content = f.read()
    
    # 匹配目标表格区域(正则可根据实际调整)
    table_pattern = re.compile(r'Top 5 Timed Foreground Events\n-+\n(.+?)\n\n', re.DOTALL)
    table_match = table_pattern.search(content)
    if not table_match:
        print("未找到目标表格")
        return
    
    table_lines = table_match.group(1).split('\n')
    # 提取表头(处理多余空格)
    headers = [h.strip() for h in re.split(r'\s{2,}', table_lines[0].strip())]
    # 提取数据行
    rows = []
    for line in table_lines[1:]:
        if line.strip() == '':
            continue
        # 按多空格拆分列值
        row_data = [d.strip() for d in re.split(r'\s{2,}', line.strip())]
        rows.append(row_data)
    
    # 写入CSV
    with open(output_csv, 'w', newline='', encoding='utf-8') as csv_f:
        writer = csv.writer(csv_f)
        writer.writerow(headers)
        writer.writerows(rows)

# 调用示例
parse_awr_text_to_csv('awr_report.text', 'awr_top_events.csv')

2. 读取HTML格式Oracle AWR报告的代码片段

HTML格式的AWR报告结构更规整,可直接用BeautifulSoup解析表格数据:

from bs4 import BeautifulSoup
import csv

def read_awr_html(input_html, output_csv):
    with open(input_html, 'r', encoding='utf-8') as f:
        soup = BeautifulSoup(f, 'html.parser')
    
    # 定位目标表格(可根据表格的class或标题调整)
    target_table = soup.find('table', {'summary': 'Top 5 Timed Foreground Events'})
    if not target_table:
        print("未找到目标表格")
        return
    
    # 提取表头
    headers = [th.get_text(strip=True) for th in target_table.find_all('th')]
    # 提取数据行
    rows = []
    for tr in target_table.find_all('tr')[1:]:  # 跳过表头行
        row_data = [td.get_text(strip=True) for td in tr.find_all('td')]
        if row_data:
            rows.append(row_data)
    
    # 写入CSV
    with open(output_csv, 'w', newline='', encoding='utf-8') as csv_f:
        writer = csv.writer(csv_f)
        writer.writerow(headers)
        writer.writerows(rows)

# 调用示例
read_awr_html('awr_report.html', 'awr_html_top_events.csv')

注意:需先安装依赖库pip install beautifulsoup4

3. .lst格式报告无法读取的解决方法

.lst格式是Oracle AWR生成的纯文本报告变种,无法读取通常是以下原因及对应解决方法:

  • 编码不匹配:尝试用不同编码读取,比如GBK、UTF-8、ISO-8859-1。示例:
    # 尝试多种编码
    encodings = ['utf-8', 'gbk', 'iso-8859-1']
    for enc in encodings:
        try:
            with open('awr_report.lst', 'r', encoding=enc) as f:
                content = f.read()
                print(f"成功用{enc}编码读取")
                break
        except UnicodeDecodeError:
            continue
    
  • 文件损坏/生成异常:重新通过Oracle生成AWR报告(使用DBMS_WORKLOAD_REPOSITORY.AWR_REPORT_TEXT存储过程)。
  • 文件过大导致内存问题:分块读取文件,避免一次性加载全部内容:
    with open('awr_report.lst', 'r', encoding='utf-8', errors='ignore') as f:
        for line in f:
            # 逐行处理逻辑
            process_line(line.strip())
    
  • 格式混淆:部分.lst文件可能是带控制字符的打印格式,可先用文本编辑器(如Notepad++)打开,开启"显示所有字符"功能,查看是否有特殊控制符,再针对性处理。

内容的提问来源于stack exchange,提问作者OumniaSdn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 09:50:43