用Python重复打印几行

时间:2022-11-20 22:28:02

I'm working on a project for class that revolves around astronomy and telescope position using Python 3.6. My project involves printing the telescope's position to the console and updating repeatedly. I want to have the info printed to the console and overwritten every timestep to keep the user updated about its current position. So far, I've implemented printing out the local time and UTC:

我正在研究一个使用Python 3.6围绕天文学和望远镜位置的课程。我的项目涉及将望远镜的位置打印到控制台并反复更新。我希望将信息打印到控制台并在每个时间步骤覆盖,以使用户了解其当前位置。到目前为止,我已经实现了打印当地时间和UTC:

import time
from datetime import datetime
from numpy import array
import sys

#Initial conditions
longitude= 83.6123 #W
latitude= 41.6624 #N
tstep=1/24 #timestep between time calculations
zero_angle=latitude #when telescope is directly up, the angle between telescope and horizon is the latitude.


while stop!=1:
    utc_time,loc_time=datetime.utcnow(),time.ctime()
    print('UTC       :',utc_time,'Local time:',loc_time,flush=True,end='\r')
    #print('Coordinates: ', latitude, ' N',longitude, ' W')
    time.sleep(tstep)

Some of the stuff at the top is for later use and isn't relevant yet. This code currently prints out:

顶部的一些东西供以后使用,但尚不相关。此代码目前打印出来:

UTC       : 2018-04-27 20:41:41.981367 Local time: Fri Apr 27 16:41:41 2018

and updates that output every 1/24th of a second. What I'd like to do is have this happen with multiple lines (so several lines update every timestep, and get overwritten). The flush=True and end='\r' I found in other threads, but applying this to a separate print statement overwrites the first line. Is there a way to have several independent lines that are all updated each timestep? Thanks!

和每1/24秒输出的更新。我想要做的是用多行来实现这一点(因此几行每次更新一次,并被覆盖)。我在其他线程中找到了flush = True和end ='\ r',但将其应用于单独的print语句会覆盖第一行。有没有办法让每个时间步都更新几个独立的行?谢谢!

2 个解决方案

#1


0  

Look up the curses module. It is an interface to the classic curses library for doing cursor movement on a terminal.

查找curses模块。它是经典curses库的接口,用于在终端上进行光标移动。

#2


0  

looks like I found a solution!

看起来我找到了解决方案!

If you import "clear_output" from IPython.display you can allow the console to refresh after each iteration with updated information. The resulting code:

如果从IPython.display导入“clear_output”,则可以在每次迭代后使用更新的信息刷新控制台。结果代码:

while im_still_presenting==True:
    utc_time,loc_time=datetime.utcnow(),time.ctime()
    print('UTC        :',utc_time,'Julian Day:',JD(utc_time),flush=True)
    print('Local time :',loc_time,flush=True)
    print('Coordinates: ', latitude, ' N',longitude, ' E',flush=True)

    time.sleep(tstep)
    clear_output(wait=True)

will print this to the console:

将打印到控制台:

UTC        : 2018-04-29 15:17:30.667045 Julian Day: 2458237.5
Local time : Sun Apr 29 11:17:30 2018
Coordinates:  41.6624  N -83.6123  E

and update the UTC and local time fields every timestep. Thanks for your help everybody :)

并在每个时间步更新UTC和本地时间字段。谢谢大家的帮助:)

#1


0  

Look up the curses module. It is an interface to the classic curses library for doing cursor movement on a terminal.

查找curses模块。它是经典curses库的接口,用于在终端上进行光标移动。

#2


0  

looks like I found a solution!

看起来我找到了解决方案!

If you import "clear_output" from IPython.display you can allow the console to refresh after each iteration with updated information. The resulting code:

如果从IPython.display导入“clear_output”,则可以在每次迭代后使用更新的信息刷新控制台。结果代码:

while im_still_presenting==True:
    utc_time,loc_time=datetime.utcnow(),time.ctime()
    print('UTC        :',utc_time,'Julian Day:',JD(utc_time),flush=True)
    print('Local time :',loc_time,flush=True)
    print('Coordinates: ', latitude, ' N',longitude, ' E',flush=True)

    time.sleep(tstep)
    clear_output(wait=True)

will print this to the console:

将打印到控制台:

UTC        : 2018-04-29 15:17:30.667045 Julian Day: 2458237.5
Local time : Sun Apr 29 11:17:30 2018
Coordinates:  41.6624  N -83.6123  E

and update the UTC and local time fields every timestep. Thanks for your help everybody :)

并在每个时间步更新UTC和本地时间字段。谢谢大家的帮助:)