如图所示,python脚本如何清空之前的输出到光标位置?

不需要清理这个位置
如图所示,python脚本如何清空之前的输出到光标位置?

第二个进度条出现的时候 清空如图所示区域

如图所示,python脚本如何清空之前的输出到光标位置?

如图所示,python脚本如何清空之前的输出到光标位置?

为什么无法清理为什么用交互式命令后inquirer向清理之前的,却无法清理

import importlib

import inquirer

from crontabs import Crontab

if __name__ == '__main__':

# Tasks.four_screen()

# result_dataframe = pd.read_excel(f'assets{os.sep}旗舰店链接汇总.xlsx').merge(

# pd.read_csv(os.path.join('cache', 'sales_data.csv')), on='店铺')

# data = result_dataframe[result_dataframe['业绩归属'] != '其他'][

# ['业绩归属', '昨日销售量', '昨日销售额', '本月销售量', '本月销售额', '销量目标', '销售额目标']]

# print(data)

# result_dataframe.to_csv('aaa.csv')

# exit(0)

crawler_type = [

inquirer.List(

"crawler",

message="请选择工具",

choices=["semrush",

'网站截图工具',

'商品价格监控',

'领星业绩统计',

'亚马逊',

'定时任务',

'更新驱动',

# '版本更新'

],

),

]

try:

crawler_name = inquirer.prompt(crawler_type)['crawler']

if crawler_name == 'semrush':

module = importlib.import_module("crawler.semrush.main")

module.control_panel()

elif crawler_name == '更新驱动':

from foundation.selenium.update_chrome_driver import download

download()

elif crawler_name == '商品价格监控':

from crawler.price_monitor import run

run()

elif crawler_name == '领星业绩统计':

from office.lingxing import performance

elif crawler_name == '定时任务':

print("\r", end='', flush=True)

Crontab().control_panel()

elif crawler_name == '亚马逊':

pass

except TypeError:

pass

import time

import inquirer

from apscheduler.schedulers.background import BackgroundScheduler

from crontabs.Tasks import Tasks

from concurrent.futures import ThreadPoolExecutor

def test():

Tasks.four_screen()

executor = ThreadPoolExecutor(max_workers=5) # 这里设置最大工作线程数为 5,可以根据需求进行调整

scheduler = BackgroundScheduler(executor=executor)

scheduler.add_job(Tasks.ling_xing_order_and_listing, 'cron', name="每小时运行领星订单和listing", hour='1-23',

minute='0', second='0')

scheduler.add_job(Tasks.four_screen, 'cron', name="领星截屏", day_of_week='1-5', hour='8,13,17', minute='0',

second='0')

scheduler.add_job(Tasks.dingding, 'cron', name="业绩推送", args=[], month='*', day='*', hour='5')

scheduler.add_job(test, 'interval', seconds=1,max_instances=1)

class Crontab:

@staticmethod

def control_panel():

menu = inquirer.prompt([

inquirer.List(

'menu',

message="执行什么操作?",

choices=["开始运行", "查看列表"],

),

])['menu']

if menu == "开始运行":

try:

scheduler.start()

while True:

pass

except (KeyboardInterrupt, SystemExit):

# 在接收到 KeyboardInterrupt 或 SystemExit 信号时,关闭调度器

scheduler.shutdown()

if menu == "查看列表":

scheduler.print_jobs()


回答:

\r 可以做到。运行下面代码看看是不是你要的效果:

python">import time

for i in range(10):

print(f"{i=}", end="\r", flush=True)

time.sleep(1)

或者通过ANSI控制字符来控制终端的字符行为:

import sys

def clear_console():

# 这个会将整个缓存行全部清理,鼠标无法下滑显示上方的内容

# sys.stdout.write("\033[2J\033[3J\033[H") # 改动版本

# 这个就单纯是清屏,但是不同终端表现有点不同,vscode下的终端可以达到清理的效果,

# 鼠标滑动,没有历史遗留;但是mac终端或者其他终端显示,只是将上方内容向上做了隐藏

# 鼠标滑动,还能看到原先的内容

sys.stdout.write("\033[2J\033[H")

把这个函数插入到你想使用的地方就可以实现清屏。

  • ESC - sequence starting with ESC (\x1B) or (\033)
ESC Code SequenceDescription
ESC[Jerase in display (same as ESC[0J)
ESC[0Jerase from cursor until end of screen
ESC[1Jerase from cursor to beginning of screen
ESC[2Jerase entire screen
ESC[3Jerase saved lines
ESC[Kerase in line (same as ESC[0K)
ESC[0Kerase from cursor to end of line
ESC[1Kerase start of line to the cursor
ESC[2Kerase the entire line

除此之外,你还可以实现针对行的清除,我曾经有实现过这个逻辑,代码粘贴给你:

import sys

def clear_lines_(num_lines: int):

for _i in range(num_lines, -1, -1):

# Move the cursor up 'num_lines' lines

sys.stdout.write("\033[1A")

# Clear the lines

sys.stdout.write("\033[2K")

# Move the cursor back to the beginning of the first cleared line

sys.stdout.write("\033[{}G".format(0))

ANSI字符集的资料很多,这里我贴出来一个 ANSI Escape Sequences

以上是 如图所示,python脚本如何清空之前的输出到光标位置? 的全部内容, 来源链接: utcz.com/p/939095.html

回到顶部