栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python怎么打印出带颜色的字符串--2-rich

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python怎么打印出带颜色的字符串--2-rich

 这个有rich库,可以去github上看

https://github.com/Textualize/rich

"Textualize/rich: Rich is a Python library for rich text and beautiful formatting in the terminal. (github.com)

这个有rich库,可以去github上看,提供中文以及好几种方式

示例:

# 有几种方法可以为输出添加颜色和样式。您可以通过添加style关键字参数来为整个输出设置样式。例子如下:
#
# console.print("Hello", "World!", )
###########第一种用法################
from rich.console import Console

# 在终端中可以使用
console=Console()

console.print("Hello","world",)    #打印红色字体
console.print("Hello","world",)


###########第二种用法################

from  rich import print as rich_print

version="v2.9版本"
version1="v2.91版本"
rich_print(f'[red]{version}  {version1}[/red]')    #红色
rich_print(f'[green]{version}[/green]')
rich_print(f'[blue]{version}[/blue]')

###########第三种用法---表格操作###############################################33
# Rich 可以使用 Unicode 框字符来呈现多变的表格。Rich 包含多种边框,样式,单元格对齐等格式设置的选项。下面是一个简单的示例:

from rich.console import Console
from rich.table import Column, Table

console = Console()

table = Table(show_header=True, header_)
table.add_column("Date", , width=12)
table.add_column("Title")
table.add_column("Production Budget", justify="right")
table.add_column("Box Office", justify="right")
table.add_row(
    "Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$275,000,000", "$375,126,118"
)
table.add_row(
    "May 25, 2018",
    "[red]Solo[/red]: A Star Wars Story",
    "$275,000,000",
    "$393,151,347",
)
table.add_row(
    "Dec 15, 2017",
    "Star Wars Ep. VIII: The Last Jedi",
    "$262,000,000",
    "[bold]$1,332,539,889[/bold]",
)

console.print(table)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/966371.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号