将Between字符串转换为整型数据类型



我正试图将木星的距离转换为AU(天文单位)光年,我使用的是预先存在的模块,这意味着您对数据类型没有偏好,我有以下错误。

我正在使用模块Skyfield (Skyfield。api)和Scipy我的代码:

from scipy import constants
from skyfield.api import load
planets = load('de421.bsp')
earth, jupiter = planets['earth'], planets['JUPITER BARYCENTER']
ts = load.timescale()
t = ts.now()
astrometric = earth.at(t).observe(jupiter)
radec=astrometric.radec()
# int(constants.astronomical_unit / constants.light_year ) * int(str(radec[2])
# Since the above line is not working i tried this:
int(constants.astronomical_unit / constants.light_year ) * int(str(radec[2]).replace("au", "").strip())




错误:

int(常量。Light_year/constants.astronomical_unit) * int(str(radac [2]).replace("au", "))ValueError: int()以10为基数的无效文字:'4.63954'

我起初认为空格可能是原因,但即使当我应用strip()函数时,错误仍然存在

my Python version isPython 3.9.12

试着用int(radec[2].au)代替int(str(radec[2])),这样就变成:

int(constants.light_year / constants.astronomical_unit) * int(radec[2].au))

如果你打印它,你得到252964

注意:您应该考虑在浮点数上进行所有计算,并在最后将答案转换为int:

print(int(float(constants.light_year / constants.astronomical_unit) * float(radec[2].au)))

给出293505.

尝试将radac[2]的字符串值转换为浮点数,然后再将其转换为整数

int(constants.light_year / constants.astronomical_unit) * 
int(float(str(radec[2]).replace("au", "").strip()))

最新更新