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

循环浏览xml文件列表?

  •  1
  • chrlo  · 技术社区  · 6 年前

    我正在尝试创建一个程序,该程序循环遍历xml文件列表,并从文件中提取某些元素:

    from os import listdir, path
    import xml.etree.ElementTree as ET
    
    mypath = 'C:\myfolder'
    
    files = [f for f in listdir(mypath) if f.endswith('.xml')]
    
    for file in files:
        tree = ET.parse(file)
        root = tree.getroot()
    
    ns = {namespaces}
    
    def myfunction():
        if 'something' in root.tag:
            filename = path.splitext(file)[0]
            var1 = root.find('./element1', ns)
            var2 = root.find('./element2', ns)
    
            row = [
                var1.text,
                var2.text
                ]
    
        return row   
    

    如果调用该函数,上述代码将返回一个包含var1、var2(来自上一个文件)的列表。我定义此函数的原因是,有不同类型的xml文件具有不同的元素名称,因此我将为每种文件类型创建一个函数。

    现在,我想创建一个表,其中每个文件的输出是一行,即:

    filename1, var1, var2
    filename2, var1, var2
    ect.
    

    理想情况下,将表导出到csv文件。我该怎么办?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Adriano Martins    6 年前

    编写CSV文件的最简单方法是使用 Standard CSV 。 要编写CSV文件,只需打开文件并使用默认编写器即可:

    import csv
    from os import listdir, path
    import xml.etree.ElementTree as ET
    
    mypath = 'C:\myfolder'
    
    files = [f for f in listdir(mypath) if f.endswith('.xml')]
    
    for file in files:
        tree = ET.parse(file)
        root = tree.getroot()
    
    ns = {namespaces}
    
    def myfunction():
        if 'something' in root.tag:
            filename = path.splitext(file)[0]
            var1 = root.find('./element1', ns)
            var2 = root.find('./element2', ns)
    
            row = [
                var1.text,
                var2.text
                ]
    
            # Open the file and store the data
            with open('outfile.csv', 'a', newline='') as csvfile:
                csv_writer = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
                csv_writer.writerow(row)
    
        return row   
    

    请注意 csf.writer 接收列表作为参数。