0

First of all I am a newbie, and has no background on coding.

i am trying to create a python script that will create an xml api file which can be used later,

i have a csv file where each line is for one api call. I might have 200+ such lines. I am trying to read one line at time and print the xml data into a file, lets say file-1.xml

I can print the data with out any issues, but I could not find a way to redirect this output a file which should be named iteratively (e.g for i in range(1..200) or something)

#!/usr/bin/python
import csv
with open('file.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        length=len(row)
        print """<?xml version="1.0" encoding="UTF-8" ?>
      <value>
       <array>
        <data>
         <value><i4>%s</i4></value>
         <value><i4>%s</i4></value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value><i4>%s</i4></value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value>%s</value>
         <value><i4>%s</i4></value>
         <value>%s</value>
        </data>
       </array>
      </value>
     </member>
    </struct>
   </value>
  </param>
 </params>
</methodCall>
                    """ % tuple(row)

2 Answers 2

1

The csv.reader class has a line number attribute for each row: line_num. Simply use reader.line_num as part of the file name like

for row in reader:
    with open('file-{0}.xml'.format(reader.line_num), 'w+') as out:
        xml_template = "..."    # Put your xml template here
        out.write(xml_template % tuple(row))

No fancy extra code needed. This does assume, though, that each request is on its own, single line.

Also, with new formatting guidelines, it is customary to use the {0} and .format style of string formatting. So instead of %s all through your template and % tuple(row) at the end, use {0} and .format(tuple(row)) at the end. Just a suggestion, though. See PEP 3101.

Sign up to request clarification or add additional context in comments.

Comments

0
import csv
with open('file.csv', 'r') as f:
    reader = csv.reader(f)
    for i, row in enumerate(reader, start=1):
        with open('file-{}.xml'.format(i), 'w+') as outfile:
            outfile.write(pattern % tuple(row)) #for brevity omitted template

You just need to open the file and write to it, much like you read from files. To keep track of which number we're on, we'll use enumerate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.