python rich再一行一左一右输出table
时间: 2024-05-06 20:17:28 浏览: 18
可以使用Rich库中的`Table`模块来实现一行一左一右输出的表格。具体代码如下:
```python
from rich.table import Table
from rich.console import Console
console = Console()
table = Table(show_header=True, header_style="bold magenta")
table.add_column("Left Column", justify="left", style="cyan", no_wrap=True)
table.add_column("Right Column", justify="right", style="green")
table.add_row("This is left aligned", "This is right aligned")
table.add_row("Another left aligned", "Another right aligned")
console.print(table)
```
输出结果如下:
```
┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━┓
┃ [cyan]Left Column [/cyan]┃ [green]Right Column [/green]┃
┣━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━┫
┃ This is left aligned ┃ This is right aligned ┃
┠━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━┨
┃ Another left aligned┃ Another right aligned┃
┗━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━┛
```
可以看到,左边的列使用了青色,右边的列使用了绿色,且第一行输出的是表头。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)