gpt4 book ai didi

python - 从 Python 中的 pyephem 日期字符串中提取数据

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:26 25 4
gpt4 key购买 nike

我想提取日出时间,如果我执行以下操作

sun = ephem.Sun()
r1 = home.next_rising(sun)
print ("Visual sunrise %s" % r1)
risehr = r1[10:12]
print ("Rise Hour = %s" % risehr)

我得到了错误

>>'ephem.Date' object has no attribute '__getitem__'

我可以打印字符串 r1 但不能从中提取(?)我尝试了有关提取的类似问题帖子的解决方案,但没有取得任何进展,如果这看起来是双重帖子,我深表歉意。

最佳答案

据我了解您的问题,您只想打印日出时间。 r1ephem.Date type 的对象.你可以用蛮力做到这一点

...
risehr = str(r1)[10:12]
...

或者您可以将 r1 对象转换为 datetime,将 datetime 转换为 str 表示

...
risehr = r1.datetime().strftime('%H')
...

或者先转换为tuple

...
risehr = r1.tuple()[3]
...

您可以在 this page 阅读所有可用选项在 Conversions 部分。

关于python - 从 Python 中的 pyephem 日期字符串中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43376336/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com