《Disney +》 迪士尼、Marvel、彼思、星球大戰…  獎金總額1,000萬日幣!由專業6團體進行電競比賽「決勝時刻 專業對抗戰」正式開打!   《Destruction AllStars》七月份更新今日到位   首屆奧運虛擬系列賽賽車項目將於6月23日轉播   《Super Animal Royale》本年稍後將為PS4和PS5帶來超萌大亂鬥   今年冬天也一起線上同樂吧!「RTA in Japan 2020」將於2月27日開幕!   CAPCOM官方節目將在東京電玩展首日登場!CAPCOM TGS2021 Online網站開幕!   《漫威復仇者聯盟》戰情桌為你重點整理瓦干達   PlayStation獨立遊戲優惠活動於PlayStation Store隆重登場 

Termgraph:於 CLI 畫出柱狀圖的程式

商業

Linux 在 CLI 想要將數據資料化成柱狀圖(Bar chart),可以使用 Termgraph 來達成。

Termgraph:於 CLI 畫出柱狀圖的程式

Termgraph 是 Python3 寫的 Script 程式,原始碼可見:

於 Debian、Ubuntu Linux 使用 pip3 安裝步驟:

  1. apt-get install python3-pip
  2. pip3 install termgraph # 到此就安裝完成
  3. termgraph data/ex1.dat # 此 data 於 GitHub 取得,此行就可以將柱狀圖畫出
  4. cat data/ex1.dat # 中間可用 空白、"," 分隔,ex1 ~ ex8 有各種範例,可以有 title、比例、顏色等等
    # Example Data Set 1
    2007 183.32
    2008 231.23
    2009 16.43
    2010 50.21
    2011 508.97
    2012 212.05
    2014 1.0

Termgraph 的問題處理

  1. $ termgraph data/ex1.dat
    • 若遇到這個問題:UnicodeEncodeError: 'ascii' codec can't encode character 'u2587' in position 0: ordinal not in range(128)
    • 解法:export PYTHONIOENCODING=utf-8
  2. 指定 export PYTHONIOENCODING=utf-8 後,跑:termgraph data/ex1.dat --custom-tick "🏃" --width 20 --title "Running Data"
    • 出現此錯誤:UnicodeEncodeError: 'utf-8' codec can't encode characters in position 0-3: surrogates not allowed
    • 解法:unset PYTHONIOENCODING

遇到上述問題,乾脆比較保險的作法,先 unset PYTHONIOENCODING,需要 UTF-8 設定時,直接當行補上即可:

  • $ PYTHONIOENCODING=utf-8 termgraph data/ex1.dat
  • $ termgraph data/ex1.dat --custom-tick "🏃" --width 20 --title "Running Data"
  • 這樣子就都可以正常顯示囉~
Tsung

隨機商業新聞

Gearbest