Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在派弗海姆查行星或月亮的名字?_Python_Pyephem - Fatal编程技术网

Python 在派弗海姆查行星或月亮的名字?

Python 在派弗海姆查行星或月亮的名字?,python,pyephem,Python,Pyephem,我正在编写一个小的pyephem程序,其中用户以行星或月球的名义通过,然后程序对其进行一些计算。我找不到如何按名称查找行星或月球,就像查找恒星(以弗所星(“大角星”)一样),所以我的程序目前有一个行星和月球名称的查找表。皮耶芬能做到吗?如果没有,是否值得添加?您可以找到一个教程 例如: >>> import ephem >>> u = ephem.Uranus() >>> u.compute('1781/3/13') >>>

我正在编写一个小的pyephem程序,其中用户以行星或月球的名义通过,然后程序对其进行一些计算。我找不到如何按名称查找行星或月球,就像查找恒星(以弗所星(“大角星”)一样),所以我的程序目前有一个行星和月球名称的查找表。皮耶芬能做到吗?如果没有,是否值得添加?

您可以找到一个教程

例如:

>>> import ephem
>>> u = ephem.Uranus()
>>> u.compute('1781/3/13')
>>> print u.ra, u.dec, u.mag
5:35:45.28 23:32:54.1 5.6
>>> print ephem.constellation(u)
('Tau', 'Taurus')

我想你可以在那里找到更多的细节。

一个有趣的问题!确实存在一种内部方法,底层的
\u libastro
使用它来告诉
ephem
自己支持哪些对象:

import ephem
from pprint import pprint
pprint(ephem._libastro.builtin_planets())
其中打印:

[(0, 'Planet', 'Mercury'),
 (1, 'Planet', 'Venus'),
 (2, 'Planet', 'Mars'),
 (3, 'Planet', 'Jupiter'),
 (4, 'Planet', 'Saturn'),
 (5, 'Planet', 'Uranus'),
 (6, 'Planet', 'Neptune'),
 (7, 'Planet', 'Pluto'),
 (8, 'Planet', 'Sun'),
 (9, 'Planet', 'Moon'),
 (10, 'PlanetMoon', 'Phobos'),
 (11, 'PlanetMoon', 'Deimos'),
 (12, 'PlanetMoon', 'Io'),
 (13, 'PlanetMoon', 'Europa'),
 (14, 'PlanetMoon', 'Ganymede'),
 (15, 'PlanetMoon', 'Callisto'),
 (16, 'PlanetMoon', 'Mimas'),
 (17, 'PlanetMoon', 'Enceladus'),
 (18, 'PlanetMoon', 'Tethys'),
 (19, 'PlanetMoon', 'Dione'),
 (20, 'PlanetMoon', 'Rhea'),
 (21, 'PlanetMoon', 'Titan'),
 (22, 'PlanetMoon', 'Hyperion'),
 (23, 'PlanetMoon', 'Iapetus'),
 (24, 'PlanetMoon', 'Ariel'),
 (25, 'PlanetMoon', 'Umbriel'),
 (26, 'PlanetMoon', 'Titania'),
 (27, 'PlanetMoon', 'Oberon'),
 (28, 'PlanetMoon', 'Miranda')]
您只需要这三项中的最后一项,因此可以构建一个名称列表,如:

>>> pprint([name for _0, _1, name in ephem._libastro.builtin_planets()])
返回:

['Mercury',
 'Venus',
 'Mars',
 'Jupiter',
 'Saturn',
 'Uranus',
 'Neptune',
 'Pluto',
 'Sun',
 'Moon',
 'Phobos',
 'Deimos',
 'Io',
 'Europa',
 'Ganymede',
 'Callisto',
 'Mimas',
 'Enceladus',
 'Tethys',
 'Dione',
 'Rhea',
 'Titan',
 'Hyperion',
 'Iapetus',
 'Ariel',
 'Umbriel',
 'Titania',
 'Oberon',
 'Miranda']

然后,您可以通过一个简单的
getattr(ephem,name)
调用,获取这些对象中的任何一个,给定其
name

我的问题是,是否可以在不使用我自己的查找表的情况下,以字符串形式传入行星的名称,并构造相应的pyephem对象。
import ephem
from ephem import *

## Planet name plus () and return
## just to show what the name must be

buscar = 'Jupiter()' + '\n'
aqui = city('Bogota')
aqui.date = now() - 5/24     ## Substract the time zone hours from UTC


if buscar[-3:-1] == '()':    ## Delete unwanted chars
    astro = buscar[:-3]
    cuerpo = getattr(ephem, astro)() ## YOUR ANSWER

## Body test
cuerpo.compute(aqui)
print(aqui.name, aqui.date)
print(cuerpo.name, cuerpo.az, cuerpo.alt)