22 ตุลาคม 2559

Published ตุลาคม 22, 2559 by with 0 comment

สร้างตารางข้อมูลใน terminals ด้วย Python

ในการเขียนโปรแกรมสำหรับดูแลรักษาระบบ ผู้ดูแลระบบหลายคนคงใช้งาน Linux server และ Unix server โดยที่ไม่ได้ติดตั้ง GUI
เพื่อประหยัดแรมและเพื่อประสิทธิภาพการทำงานให้กับเครื่อง จึงทำให้ผู้ดูแลระบบบางคนไม่เลือกที่จะใช้ GUI บทความนี้ผมจะมาแนะนำโมดูลสำหรับสร้างตารางข้อมูลใน terminals ด้วย Python กันครับ

ขอแนะนำโมดูล terminaltables เป็นโมดูลสำหรับใช้สร้างตารางข้อมูลใน terminals ด้วยภาษา Python

  • รองรับทั้ง Python 2 และ Python 3

  • รองรับทั้ง Windows, Linux และ OS X

  • License: MIT

การติดตั้ง
pip install terminaltables

การใช้งาน
สามารถอ่านเอกสารได้ที่ https://github.com/Robpol86/terminaltables
สร้างตารางข้อมูลใน terminals ด้วย Python

from terminaltables import AsciiTable
table_data = [
['Heading1', 'Heading2'],
['row1 column1', 'row1 column2'],
['row2 column1', 'row2 column2'],
['row3 column1', 'row3 column2']
]
table = AsciiTable(table_data)
print(table.table)

ผลลัพธ์
+--------------+--------------+
| Heading1     | Heading2     |
+--------------+--------------+
| row1 column1 | row1 column2 |
| row2 column1 | row2 column2 |
| row3 column1 | row3 column2 |
+--------------+--------------+

0 ความคิดเห็น:

แสดงความคิดเห็น

แสดงความคิดเห็นได้ครับ :)