sauce1173
sauce1173

Reputation: 65

python formatting issue parsing child element

I am parsing an xml file that lists some servers

example xml for 2 server:

<cluster-information>
    <clustering-available>true</clustering-available>
    <clustered>true</clustered>
    <node>
      <id>SomeIDnum</id>
      <address>/someIP:port</address>
      <local>false</local>
    </node>
    <node>
      <id>SomeIDnum</id>
      <address>/someIP:port</address>
      <local>false</local>
    </node>
  </cluster-information>

I am using the following to get the ID and Address

cluster=myroot.find('cluster-information/clustered')
if cluster.text == 'true':
    print("|Cluster is "+cluster.text+" |")
    nodes=myroot.find('cluster-information')
    for x in nodes.findall('node'):
        id=x.find('id')
        ip=x.find('address')
        print("Node:"+id.text)
        print("IP "+ip.text)
    print("|")

The result is:

|Cluster is true | Node:someID IP /x.x.x.x:port Node:someotherID IP /x.x.x.x:port |

I need to the output to look like this: |Cluster is true |Node:someID IP /x.x.x.x:port Node:someotherID IP /x.x.x.x:port |

Basically I need to remove the first newline that is created form the for loop.

Upvotes: 4

Views: 28

Answers (1)

Armali
Armali

Reputation: 19375

I need to remove the first newline that is created form the for loop.

That first newline isn't created form the for loop - it is generated by the

    print("|Cluster is "+cluster.text+" |")

before the loop. To remove it, change to

    print("|Cluster is "+cluster.text+" |", end="")

Upvotes: 2

Related Questions