简体   繁体   English

如何使用python转换csv文件中的文件夹中的多个xml文件?

[英]How convert multiple xml files in a folder in csv files with python?

1-I ready have a code than convert de xml file to csv. 1-我准备好将xml文件转换为csv的代码。 But actually I need to do this operation a lot times and then a need to change the name of the xml file in my program each time.I want to do a loop to read each '.xml' file in the folder location and do for all file. 但是实际上我需要多次执行此操作,然后每次都需要在程序中更改xml文件的名称。我想做一个循环以读取文件夹位置中的每个'.xml'文件并为所有文件。 there is my original code 有我的原始密码

import xml.etree.ElementTree as ET
import csv

tree = ET.parse("Shot_30AA.xml")
root = tree.getroot()

Shot_30AA = open('Shot_30AA.csv', 'w', newline='')
csvwriter = csv.writer(Shot_30AA)
head = []

ShotCode = root.attrib['ShotCode']
csvwriter.writerow(['ShotCode', ShotCode])
head.append(ShotCode)

for member in root.getchildren():
  submembers = member.getchildren()
  keys = submembers[0].attrib.keys()
  csvwriter.writerow ("\n")
  csvwriter.writerow(keys)
  for submember in submembers:
    row_data = [submember.attrib[k] for k in keys]
    csvwriter.writerow(row_data )
Shot_30AA.close()

I tried to do this operation in my file folder adding the fallowing code to do only one time 我试图在我的文件夹中执行此操作,添加休闲代码仅执行一次

path = 'C:/Users/Desktop/Program'
for filename in os.listdir(path):
   if not filename.endswith('.xml'): continue
     ShotFile = os.path.join(path, filename)
     tree = ET.parse(ShotFile)
     root = tree.getroot()

     filename = open( filename'.csv', 'w', newline='')
     csvwriter = csv.writer(filename)
     head = [] 

     ShotFile = open('ShotFile.csv', 'w', newline='')
     csvwriter = csv.writer(Shot_30AA)
     head = []

     ShotCode = root.attrib['ShotCode']
     csvwriter.writerow(['ShotCode', ShotCode])
     head.append(ShotCode)

     for member in root.getchildren():
       submembers = member.getchildren()
       keys = submembers[0].attrib.keys()
       csvwriter.writerow ("\n")
       csvwriter.writerow(keys)
       for submember in submembers:
         row_data = [submember.attrib[k] for k in keys]
         csvwriter.writerow(row_data )
  ShotFile.close()

I expect to obtain all my files in csv format with one run. 我希望一次运行即可获得csv格式的所有文件。

Here's what could work based on your code 这是根据您的代码可能起作用的

def make_csv(folderpath, xmlfilename):
    tree = ET.parse(os.path.join(folderpath, xmlfilename))
    root = tree.getroot()
    filename, _ = xmlfilename.rsplit('.', 1)
    Shot_30AA = open(filename+'.csv', 'w', newline='')
    csvwriter = csv.writer(Shot_30AA)
    head = []

    ShotCode = root.attrib['ShotCode']
    csvwriter.writerow(['ShotCode', ShotCode])
    head.append(ShotCode)

    for member in root.getchildren():
        submembers = member.getchildren()
        keys = submembers[0].attrib.keys()
        csvwriter.writerow("\n")
        csvwriter.writerow(keys)
        for submember in submembers:
            row_data = [submember.attrib[k] for k in keys]
            csvwriter.writerow(row_data)
    Shot_30AA.close()

path = 'C:/Users/Desktop/Program'
for filename in os.listdir(path):
    if filename.endswith('.xml'):
        make_csv(path, filename)

EDIT 1: As the comments pointed out, you should also take a look at the with open() as clause when dealing with files 编辑1:正如评论所指出的,在处理文件时,您还应该查看with open()as子句

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM