Balaji
Balaji

Reputation: 1895

Python file.next() is messing the for loop

I tried my best to search for the answer, but couldn't fine something that will suit my needs. I am trying to reformat an XML file into a custom format and running into issues. Here is my requirement.

I have the following XML file:

<parameters>
  <parameter name="name1" value="value1"/>
  <parameter name="name2" value="value2"/>
  <parameter name="name3" value="value3"/>
  <parameter name="name4" value="value4"/>
</parameters>

I am looking to convert that into the following.

(param().name("name1").value("value1"),
param().name("name2").value("value2"),
param().name("name3").value("value3"),
param().name("name4").value("value4"));

I tried using the following code and it seems to be skipping some data from the original file to the output.

with open("myfile","r") as f:
    for each_line in f:
        current_line = each_line
        if current_line.strip().startswith('<'):
            split_line = current_line.strip().split(' ')
            if split_line[0].lstrip('<') == "parameter":
               if f.next().strip() == "</parameters":
                  print 'param().name(' + split_line[1].split('=')[1] + ').value('+ split_line[2].split('=')[1][:-2] + '));\n\n'
               else:
                  print 'param().name(' + split_line[1].split('=')[1] + ').value('+ split_line[2].split('=')[1][:-2] + ')'

I see that using f.next() is causing the issue... Not sure how else can I solve this.

Upvotes: 2

Views: 214

Answers (2)

Mark Byers
Mark Byers

Reputation: 837946

You probably should be using an XML parser for this.

To fix your code you could store the results in a list and join it together and print it at the end:

result = []

with open("myfile","r") as f:
    for each_line in f:
        current_line = each_line.strip()
        if current_line.startswith('<parameter '):
            split_line = current_line.split(' ')     # Breaks if there are spaces in name or value.
            name = split_line[1].split('=')[1]       # Yuck.
            value = split_line[2].split('=')[1][:-2] # Yuck.
            result.append('param().name({0}).value({1})'.format(name, value)

print '(' + ',\n'.join(result) + ');\n\n'

Note that the way you are finding the strings inside the XML is not robust and small changes to the document will give problems. Using an XML parser would make it less likely to break.

Related

Upvotes: 5

zmo
zmo

Reputation: 24812

well, f.next() is just doing one more iteration and thus changes current line iterator. That's not what you seem to want.

but what don't you just use an xml parser ?

Upvotes: 0

Related Questions