Tuesday, 17 February 2015

Python: xml.etree.ElementTree.parse outputs text and blocks output stream



I tried getting some XML via HTTP request to a server and then parse the XML using xml.etree.ElementTree.parse.


However, for some reason the program outputs the whole XML in the console and blocks the output stream afterwards.


Some help in solving the problem would be greatly appreciated. I post the code I used for testing.



import requests
import xml.etree.ElementTree as etree
import sys

TOPCODER_BASE_URL = 'http://ift.tt/17lkLOa'

def getWebsite(url):
response = requests.get(url)
return response.text

def getTopCoderRound(rnd):
response = getWebsite(TOPCODER_BASE_URL + rnd)
return response

def loadData(outputFile):
handleFile = sys.argv[2]

f = open(handleFile, 'r')
outputFile.write ("#opened file")
users = []
for line in f:
users.append(line)

outputFile.write("#added users")
return sys.argv[1], users

outputFile = open('results.txt', 'w')

outputFile.write("reached this point")

roundNo, users = loadData(outputFile)
outputFile.write("#loaded data")
xml = getTopCoderRound(roundNo)
outputFile.write("#read xml")
tree = etree.parse(xml)

outputFile.write("reached this point")
root = tree.getroot()

No comments:

Post a Comment