代码之家  ›  专栏  ›  技术社区  ›  Vinnie

用Python从RDL中提取数据集和查询数据

  •  1
  • Vinnie  · 技术社区  · 16 年前

    我可以使用此脚本创建包含以下列的CSV文件: 系统路径|报告文件名|命令类型|命令文本|

    它不是很优雅,但很管用。

    我希望这篇文章能为你们中的任何一位专家提供帮助,他们要么已经尝试过这种方法,要么在使用Python进行XML解析方面有经验,希望他们能够尝试一下清理方法,并提供以下能力:

    • 包含头,这将是XML标记
    • 在列中包含数据集名称
    • 将结果传递到单个文件中

    以下是我的“rdlparser.py”文件中的完整代码:

    import sys, os
    
    from xml.dom import minidom
    xmldoc = minidom.parse(sys.argv[1])
    
    content = ""
    TargetFile = sys.argv[1].split(".", 1)[0] + ".csv"
    numberOfQueryNodes = 0
    
    queryNodes = xmldoc.getElementsByTagName('Query')
    numberOfQueryNodes = queryNodes.length -1
    
    
    while (numberOfQueryNodes > -1):
        content = content + os.path.abspath(sys.argv[1])+ '|'+ sys.argv[1].split(".", 1)[0]+ '|' 
        outputNode = queryNodes.__getitem__(numberOfQueryNodes)
        children = [child for child in outputNode.childNodes if child.nodeType==1]
        numberOfQueryNodes = numberOfQueryNodes - 1
        for node in children:
            if node.firstChild.nodeValue != '\n          ':
                if node.firstChild.nodeValue != 'true':
                    content = content + node.firstChild.nodeValue + '|'
        content = content + '\n'
    
    fp = open(TargetFile, 'wb')
    fp.write(content)
    fp.close()
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   Jason Horner    16 年前

    # The directory to search 
    $searchpath = "C:\"
    
    # List all rdl files    from the given search path recusrivley searching sub folders, store results into a variable
    $files = gci $searchpath -recurse -filter "*.rdl" | SELECT FullName, DirectoryName, Name 
    
    # for each of the found files pass the folder and file name  and the xml content
    $files | % {$Directory = $_.DirectoryName; $Name = $_.Name; [xml](gc $_.FullName)}
                # in the xml content navigate to the the DataSets Element
                | % {$_.Report.DataSets} 
                        # for each query retrieve the Report directory , File Name, DataSource Name, Command Type, Command Text output thwese to a csv file
                        | % {$_.DataSet.Query} | SELECT  @{N="Path";E={$Directory}}, @{N="File";E={$Name}}, DataSourceName, CommandType, CommandText | Export-Csv Test.csv -notype   
    
    推荐文章