FR报表数据集血缘分析py脚本

FR报表数据集血缘分析py脚本

功能简介

这是一个用于分析FR报表文件(.cpt/.frm)中数据集信息的Python代码。、可以递归搜索指定目录下的所有报表文件,并提取包含特定关键词的数据集信息。

主要特点

  • 支持递归搜索.cpt和.frm文件
  • 自动解析XML格式的报表文件
  • 提取包含"明细"和"business"关键词的数据集,可替换成自己实际需要的
  • 跑出结构化的结果

使用方法

  1. 确保已安装Python环境
  2. 运行脚本:
python get_data_set.py
  1. 根据提示输入要搜索的根目录路径
  2. 程序会自动跑出分析结果文件:dataset_analysis_result.txt

代码实现

import os
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import ParseError

def find_target_files(root_path):
    """
    递归查找指定路径下所有的.cpt和.frm文件
    
    Args:
        root_path: 根路径
    
    Returns:
        target_files: 包含所有目标文件路径的列表
    """
    target_files = []
    
    # 使用os.walk递归遍历所有子目录
    for root, dirs, files in os.walk(root_path):
        for file in files:
            # 检查文件扩展名是否为.cpt或.frm
            if file.lower().endswith(('.cpt', '.frm')):
                file_path = os.path.join(root, file)
                target_files.append(file_path)
    
    return target_files

def parse_table_data(file_path):
    """
    解析单个文件中的TableData信息
    
    Args:
        file_path: 文件路径
    
    Returns:
        results: 包含符合条件的数据集信息的列表,每个元素为(文件路径, 数据集名称, 关键词)
    """
    results = []
    keywords = ["明细", "business"]  # 要检查的关键词
    
    try:
        # 解析XML文件
        tree = ET.parse(file_path)
        root = tree.getroot()
        
        # 查找TableDataMap标签
        table_data_map = root.find('TableDataMap')
        if table_data_map is not None:
            # 遍历所有TableData标签
            for table_data in table_data_map.findall('TableData'):
                # 获取TableData的name属性
                table_name = table_data.get('name')
                if table_name:
                    # 查找Query标签
                    query_element = table_data.find('Query')
                    if query_element is not None:
                        # 获取Query标签的文本内容
                        query_text = query_element.text
                        if query_text:
                            # 检查是否包含关键词
                            for keyword in keywords:
                                if keyword in query_text:
                                    results.append((file_path, table_name, keyword))
                                    break  # 找到一个关键词就够了,避免重复记录
    
    except ParseError as e:
        print(f"解析XML文件失败: {file_path}, 错误: {e}")
    except Exception as e:
        print(f"处理文件时出错: {file_path}, 错误: {e}")
    
    return results

def process_files_and_save(root_path, output_file):
    """
    处理所有文件并将结果保存到txt文件
    
    Args:
        root_path: 要搜索的根路径
        output_file: 输出文件路径
    """
    print(f"开始搜索路径: {root_path}")
    
    # 查找所有目标文件
    target_files = find_target_files(root_path)
    print(f"找到 {len(target_files)} 个目标文件")
    
    # 存储所有结果
    all_results = []
    
    # 处理每个文件
    for file_path in target_files:
        print(f"正在处理: {file_path}")
        results = parse_table_data(file_path)
        all_results.extend(results)
    
    # 将结果写入txt文件
    with open(output_file, 'w', encoding='utf-8') as f:
        # 写入表头
        f.write("文件路径\t数据集名称\t关键词\n")
        
        # 写入数据
        for file_path, table_name, keyword in all_results:
            f.write(f"{file_path}\t{table_name}\t{keyword}\n")
    
    print(f"处理完成!共找到 {len(all_results)} 条符合条件的记录")
    print(f"结果已保存到: {output_file}")

def main():
    """
    主函数
    """
    # 设置要搜索的根路径(请根据实际情况修改)
    root_path = input("请输入要搜索的根路径: ").strip()
    
    # 检查路径是否存在
    if not os.path.exists(root_path):
        print(f"路径不存在: {root_path}")
        return
    
    # 设置输出文件路径
    output_file = "dataset_analysis_result.txt"
    
    # 开始处理
    process_files_and_save(root_path, output_file)

if __name__ == "__main__":
    main()

输出结果

程序会生成一个制表符分隔的文本文件,包含以下列:

  • 文件路径
  • 数据集名称
  • 匹配的关键词

注意事项

  • 确保有足够的文件系统权限
  • 建议在较小的目录范围内进行测试
  • 处理大量文件时可能需要较长时间
posted @ 2025-06-13 17:09  灯熄帘摇月候身  阅读(30)  评论(0)    收藏  举报