I'm looking for suggestions on how to combine the two code snippets so that they work with both python 2 and 3. The goal is to make it "neat", ideally keeping it to one line and limiting any if/else/try/except constructs.
For python 3.x
import xml.etree.ElementTree as ET, urllib.request, gzip, io
url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urllib.request.urlopen(url).read())))
For python 2.x
import xml.etree.ElementTree as ET, urllib, gzip, io
url = "https://github.com/OpenExoplanetCatalogue/oec_gzip/raw/master/systems.xml.gz"
oec = ET.parse(gzip.GzipFile(fileobj=io.BytesIO(urllib.urlopen(url).read())))
requests
. Alternately, run a test for py2/py3 and import the proper urllib urlopen as urlopen. You might look atsix
, which contains a boolean value to test if you're in Python3 or Python2:six.PY3
– Austin