简体   繁体   English

Python-遍历CSV行并以XML替换

[英]Python - Iterate through CSV rows and replace in XML

I have a CSV with values that I would like to replace inside an XML template . 我有一个CSV,其值要在XML模板内替换 Generating XMLs for each row that have filenames based on data in the same row. 为每行生成具有基于同一行中数据的文件名的XML These are basically just copies of the template with a find and replace. 这些基本上只是带有查找和替换的模板副本。 So far I have gotten the filenames to work correctly but the XML file does not replace its data according to the row instead it only replaces data from row 3, column 10. I'm looking to have it iterate through a range of rows and create new files for each. 到目前为止,我已经使文件名正常工作,但是XML文件不会根据行替换其数据,而是仅替换来自第3行第10列的数据。我希望遍历一系列行并创建它每个都有新文件。 I'm stumped as to what is going wrong here. 我对这里出了什么问题感到困惑。

CSV Snippet: CSV片段:

COLUMN_K, COLUMN_L
K02496.ai, Test
K02550.ai, Test
K02686.ai, Test
K02687.ai, Test

Existing XML Template Snippet 现有的XML模板代码段

  <gmd:resourceFormat>
    <gmd:MD_Format>
      <gmd:name>
        <gco:CharacterString>COLUMN_K</gco:CharacterString>
      </gmd:name>

Python Code Python代码

import csv

exampleFile = open('U:\PROJECTS\Technical Graphics\metadata.csv')
exampleReader = csv.reader(exampleFile)
exampleData = list(exampleReader) #CSV as list

with open('U:\PROJECTS\Technical Graphics\COLUMN_K_edited.xml') as inputfile: #template XML
    xml = inputfile.read()

with open('U:\PROJECTS\Technical Graphics\metadata.csv') as csvfile:
    for row in reader(csvfile, delimiter=';'):
        for i in range(5): #range of 5 rows
           xml = xml.replace('COLUMN_K', exampleData[i+3][10]) 
#Only taking value from row 3, COLUMN_K- Need values from row 3 on
           xml = xml.replace('COLUMN_L', exampleData[i+3][11]) 
#Only taking value from row 3, COLUMN_L- Need values from row 3 on    
            with open('U:\PROJECTS\Technical Graphics\XXX' + str((exampleData[i+3][10])) + ".xml", 'w') as outputfile: 
 #Correctly outputs multiple filenames based on COLUMN_K value
                outputfile.write(xml) #writes multiple XMLs

You have mentioned 'COLUMN_F' in the xml tag and you are trying to replace 'COLUMN_E' in the code 您在xml标记中提到了“ COLUMN_F”,并且试图替换代码中的“ COLUMN_E”

xml = xml.replace('COLUMN_E', exampleData[i+3][10]) xml = xml.replace('COLUMN_E',exampleData [i + 3] [10])

I was able to resolve this simply by moving the loop to the very top of the code. 通过将循环移到代码的最顶部,我能够解决此问题。 The problem was it was replacing the text but then could not find the "COLUMN_*" because it had been replaced with a value. 问题在于它正在替换文本,但随后找不到“ COLUMN_ *”,因为它已被替换为一个值。 Moving the loop to the top resolved this. 将循环移到顶部可以解决此问题。

import csv

    for i in range(5):  #loops through 5 rows
        exampleFile = open('U:\PROJECTS\Technical Graphics\metadata.csv')  #CSV file
        exampleReader = csv.reader(exampleFile)
        exampleData = list(exampleReader) #turns CSV into list

        with open('U:\PROJECTS\Technical Graphics\COLUMN_K_edited.xml') as inputfile:
            xml = inputfile.read() #XML template file

        with open('U:\PROJECTS\Technical Graphics\metadata.csv') as csvfile:

            for row in reader(csvfile, delimiter=';'): #defines CSV delimiter

                    with open('U:\PROJECTS\Technical Graphics\XXX' + str((exampleData[i+3][10])) + ".xml", 'w') as outputfile:
                    #Filename of XMLs based on column 10 data        
                        xml = xml.replace('COLUMN_I', str((exampleData[i+3][8])))
                        xml = xml.replace('COLUMN_K', str((exampleData[i+3][10])))


                        outputfile.write(xml) #writes 5 XML files

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

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