3

I'm using a webservice to get a certain xml file from it. It works fine with urllib2 I get the xml as fileobject. So I want to know what would be the fastest way to store that somewhere in memory or not even store just parse it.

I tried iterparse on that object and it takes too long unless I save it first in file, then iterparse takes much less time.

So now I'm using this code to store it locally first and then do with that file what I want, and I would like to know is there a fastest way of doing it, fastest way of storing files.

url = "webservice"
s = urllib2.urlopen(url)

file = open("export.xml",'wb+')
for line in s:
    file.write(line)

Thanks

2 Answers 2

10

You don't need to write line-by-line. Just write the whole thing in one go:

>>> import urllib2
>>> url = "webservice"
>>> s = urllib2.urlopen(url)
>>> contents = s.read()
>>> file = open("export.xml", 'w')
>>> file.write(contents)
>>> file.close()
Sign up to request clarification or add additional context in comments.

3 Comments

Yes I can, but the speed is actually the same, and I'm after the speed in this case
While there might be moderately faster techniques, the speed constraints here are probably network- and disk-based.
If you get an error write() argument must be str, not bytes, use wb instead of wfor the open command.
1

You can store it in a string:

content = s.read()

or a StringIO if you need file-like interface

content = cStringIO.StringIO()
content.write(s.read)

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.