乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      假如有一個專屬于python的終端工具,那絕對非他莫屬!

       Python集中營 2023-02-03 發(fā)布于甘肅

      rich是python中的一個非標(biāo)準(zhǔn)模塊,它提供了比較漂亮的富文本和不同風(fēng)格的控制臺支持。

      它能夠在控制臺中繪制出精美的數(shù)據(jù)表格/進度條以及突出語法的顯示等效果,重要的是還能結(jié)合notebook的使用方便科學(xué)計算的開發(fā)。

      rich作為python的非標(biāo)準(zhǔn)庫,我們在使用前需要使用pip的方式安裝一下。

      pip install rich

      安裝完成之后,我們可以在操作系統(tǒng)的終端看看rich模塊的效果非常的漂亮。

      python -m rich

      在做一些業(yè)務(wù)代碼開發(fā)的時候,我們可以將rich模塊導(dǎo)入進來使用它的print函數(shù)來完成對控制臺的打印。

      # It's importing the `print` function from the `rich` module.
      from rich import print

      # It's printing a string with bold magenta text.
      print('[bold magenta]我是一個rich的終端打印![/bold magenta]')

      可以發(fā)現(xiàn),我們在使用rich模塊的打印print函數(shù)加上[bold magenta][/bold magenta]之后打印出來的效果是經(jīng)過了個性化的處理的。

      注意:rich模塊這樣的效果必須是在操作系統(tǒng)的終端下面才能顯示出來,在開發(fā)工具比如說pycharm中就顯示不出來的。

      接下來,我們來說明常見的兩個業(yè)務(wù)場景中的顯示如何開發(fā),分別是在控制臺顯示表格和進度條。

      因為在一般的日志打印中表格的打印能夠幫助我們很好的觀察程序執(zhí)行過程中的數(shù)據(jù),這里著重說明一下。

      # It's importing the `Console` class from the `rich.console` module.
      from rich.console import Console

      # It's importing the `Column` and `Table` classes from the `rich.table` module.
      from rich.table import Table

      # 導(dǎo)入相應(yīng)的模塊之后,我們需要初始化Console的控制臺對象用作后面的print函數(shù)打印。

      # It's creating a Console object.
      console = Console()

      初始化rich模塊的表格對象Table,并且向表對象中加入相應(yīng)的表字段和行數(shù)據(jù)來查看效果。

      # It's creating a Table object with a header and a bold magenta header style.
      tab_ = Table(show_header=True, header_style="bold magenta")

      # It's adding a column named "姓名" with a dim style and a width of 12.
      tab_.add_column("姓名")

      # It's adding a column named "年齡" with a default style and a default width.
      tab_.add_column("年齡")

      # It's adding a column named "班級" with a default style and a default width.
      tab_.add_column("班級")

      # It's adding a column named "成績" with a default style and a default width.
      tab_.add_column("成績")

      # It's adding a row to the table.
      tab_.add_row("Python 集中營""21""1210班""99.5")

      # It's adding a row to the table.
      tab_.add_row("Python 集中營""21""1210班""99.5")

      # It's adding a row to the table.
      tab_.add_row("Python 集中營""21""1210班""99.5")

      # It's adding a row to the table.
      tab_.add_row("Python 集中營""21""1210班""99.5")

      # It's printing the table to the console.
      console.print(tab_)

      我們將表格打印的這部分代碼塊放到了test8_test.py的python文件,然后到控制臺的終端去執(zhí)行觀察一下結(jié)果。

      python -m test8_test.py

      下面來看看rich模塊的進度條是如何使用的,同樣的我們做一個讀取文件的進度顯示比較符合實際業(yè)務(wù)開發(fā)的情況。

      import rich.progress

      with rich.progress.open("data.txt""rb", description="文件讀取過程:"as file:
          results = file.readlines()

      # 文件讀取過程: ---------------------------------------- 2.1/2.1 kB 0:00:00

      「Python 集中營」,只做知識分享 !

      就是玩兒:python輕松實現(xiàn)代碼雨效果!

      python情感分析:基于jieba的分詞及snownlp的情感分析!

      python數(shù)據(jù)保存:記錄pandas數(shù)據(jù)分析完成后的輕量級數(shù)據(jù)保存!

      記錄一次關(guān)于python內(nèi)存泄露的排查!

        轉(zhuǎn)藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多