Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add distinct examples for Python 2 and 3 in the readme #1124

Merged
merged 1 commit into from
Apr 7, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,44 @@ How to access the catalogue using Python
--------------
It is very easy to access the Open Exoplanet Catalogue using python. Here is a short [snippet](https://gist.github.com/hannorein/2a069763cf114f66641c) to print out some basic planet data. No download, no installation and no external libraries are required.

If you are using python 3:

```python
# 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())))

# Output mass and radius of all planets
for planet in oec.findall(".//planet"):
print [planet.findtext("mass"), planet.findtext("radius")]
print([planet.findtext("mass"), planet.findtext("radius")])

# Find all circumbinary planets
for planet in oec.findall(".//binary/planet"):
print planet.findtext("name")
print(planet.findtext("name"))

# Output distance to planetary system (in pc, if known) and number of planets in system
for system in oec.findall(".//system"):
print system.findtext("distance"), len(system.findall(".//planet"))
print(system.findtext("distance"), len(system.findall(".//planet")))
```

If you are using python 2, replace the first three lines by
If you are using python 2:

```python
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())))

# Output mass and radius of all planets
for planet in oec.findall(".//planet"):
print [planet.findtext("mass"), planet.findtext("radius")]

# Find all circumbinary planets
for planet in oec.findall(".//binary/planet"):
print planet.findtext("name")

# Output distance to planetary system (in pc, if known) and number of planets in system
for system in oec.findall(".//system"):
print system.findtext("distance"), len(system.findall(".//planet"))
```

Data Structure
Expand Down