python - 有没有办法在 python 中重复 try 和 except 块?(找不到目标时出现属性错误)
问题描述
我经常使用 SIMBAD 服务来查找天体的坐标。我想我会使用 python 自动化这个。我的第一种方法是使用 Selenium 并让它在 Safari 中运行。但是,事实证明这比手动操作要困难一些。然而,大约两天前,我发现了 astroquery 以及它是如何内置 SIMBAD 的。所以我想我会用那个。它确实有效,但它不断引发此属性错误,并且我所有修复它的尝试都导致了一个新错误。这是代码:(ps我正在使用astroquery库((astroquery.simbad))和一个文本格式化程序,丰富):
from rich import print
from astroquery.simbad import Simbad
def ask_for(prompt, error_msg=None, _type=None):
""" While the desired prompt is not given, it repeats the prompt. """
while True:
inp = input(prompt).strip()
if not inp:
if error_msg:
print(error_msg)
continue
if _type:
try:
inp = _type(inp)
except ValueError:
if error_msg:
print(error_msg)
continue
return inp
def simbad_query():
not_target = True
while not_target:
try:
print(
'\nTo find the [blue]RA and Dec[/blue] of your target, please put it in here.')
print("If your target can't be found, it will automatically redirect you to the website to put it in again.")
target = ask_for('\nTarget name: ')
query = Simbad.query_object(f'{target}')
query.pprint()
except AttributeError:
print(
'\nThe target you gave was [red]not found[/red]. Please try again.')
print(
'\nTo find the [blue]RA and Dec[/blue] of your target, please put it in here.')
print("If your target can't be found, it will automatically redirect you to the website to put it in again.")
target = ask_for('\nTarget name: ')
query = Simbad.query_object(f'{target}')
query.pprint()
解决方案
推荐阅读
- android - 在方向更改时恢复列表视图中的项目和滚动位置
- javascript - Angular - Javascript - 错误:找不到变量:重新加载页面后的谷歌
- php - 将自定义字段价格作为产品价格分配给 WooCommerce 中的特定用户角色
- python - 在进程之间共享列表会引发错误: AttributeError: 'ForkAwareLocal' object has no attribute 'connection' when I try to use the list
- angular - 如何将 PrimgNg 选中的复选框向下传递到子组件下拉列表?
- amazon-web-services - 如何为负载均衡器后面的 AWS EC2 实例上的所有传出流量分配唯一的域名/IP
- autodesk-forge - 在树视图中选择项目时的右键单击和左键单击行为 - 版本 6 与 7
- sql-server - 将 Decode Substr Oracle 查询转换为 SQL Server
- sql - Proc Sql 输出
- jquery - 如何在选择选项下将背景图像添加到选项