python 列印 ASCII表

小吉猫發表於2024-05-06

ASCII表

+------+------+------+------+---------------------------------+
| Dec  | Hex  | Oct  | Char | Description                     |
+------+------+------+------+---------------------------------+
|   0  | 00   | 000  |      | NUL (null terminator)           |
|   1  | 01   | 001  |      | SOH (start of header)           |
|   2  | 02   | 002  |      | STX (start of text)             |
|   3  | 03   | 003  |      | ETX (end of text)               |
|   4  | 04   | 004  |      | EOT (end of transmission)       |
|   5  | 05   | 005  |      | ENQ (enquiry)                   |
|   6  | 06   | 006  |      | ACK (acknowledge)               |
|   7  | 07   | 007  |      | BEL (bell, alert)               |
|   8  | 08   | 010  |      | BS (backspace)                  |
|   9  | 09   | 011  |      | HT (horizontal tab)             |
|  10  | 0A   | 012  |      | LF (line feed, new line)        |
|  11  | 0B   | 013  |      | VT (vertical tab)               |
|  12  | 0C   | 014  |      | FF (form feed, new page)        |
|  13  | 0D   | 015  |      | CR (carriage return)            |
|  14  | 0E   | 016  |      | SO (shift out)                  |
|  15  | 0F   | 017  |      | SI (shift in)                   |
|  16  | 10   | 020  |      | DLE (data link escape)          |
|  17  | 11   | 021  |      | DC1 (device control 1, XON)     |
|  18  | 12   | 022  |      | DC2 (device control 2)          |
|  19  | 13   | 023  |      | DC3 (device control 3, XOFF)    |
|  20  | 14   | 024  |      | DC4 (device control 4)          |
|  21  | 15   | 025  |      | NAK (negative acknowledge)      |
|  22  | 16   | 026  |      | SYN (synchronous idle)          |
|  23  | 17   | 027  |      | ETB (end of transmission block) |
|  24  | 18   | 030  |      | CAN (cancel)                    |
|  25  | 19   | 031  |      | EM (end of medium)              |
|  26  | 1A   | 032  |      | SUB (substitute)                |
|  27  | 1B   | 033  |      | ESC (escape)                    |
|  28  | 1C   | 034  |      | FS (file separator)             |
|  29  | 1D   | 035  |      | GS (group separator)            |
|  30  | 1E   | 036  |      | RS (record separator)           |
|  31  | 1F   | 037  |      | US (unit separator)             |
|  32  | 20   | 040  |      | Space (space character)         |
|  33  | 21   | 041  |  !   | !                               |
|  34  | 22   | 042  |  "   | "                               |
|  35  | 23   | 043  |  #   | #                               |
|  36  | 24   | 044  |  $   | $                               |
|  37  | 25   | 045  |  %   | %                               |
|  38  | 26   | 046  |  &   | &                               |
|  39  | 27   | 047  |  '   | '                               |
|  40  | 28   | 050  |  (   | (                               |
|  41  | 29   | 051  |  )   | )                               |
|  42  | 2A   | 052  |  *   | *                               |
|  43  | 2B   | 053  |  +   | +                               |
|  44  | 2C   | 054  |  ,   | ,                               |
|  45  | 2D   | 055  |  -   | -                               |
|  46  | 2E   | 056  |  .   | .                               |
|  47  | 2F   | 057  |  /   | /                               |
|  48  | 30   | 060  |  0   | 0                               |
|  49  | 31   | 061  |  1   | 1                               |
|  50  | 32   | 062  |  2   | 2                               |
|  51  | 33   | 063  |  3   | 3                               |
|  52  | 34   | 064  |  4   | 4                               |
|  53  | 35   | 065  |  5   | 5                               |
|  54  | 36   | 066  |  6   | 6                               |
|  55  | 37   | 067  |  7   | 7                               |
|  56  | 38   | 070  |  8   | 8                               |
|  57  | 39   | 071  |  9   | 9                               |
|  58  | 3A   | 072  |  :   | :                               |
|  59  | 3B   | 073  |  ;   | ;                               |
|  60  | 3C   | 074  |  <   | <                               |
|  61  | 3D   | 075  |  =   | =                               |
|  62  | 3E   | 076  |  >   | >                               |
|  63  | 3F   | 077  |  ?   | ?                               |
|  64  | 40   | 100  |  @   | @                               |
|  65  | 41   | 101  |  A   | A                               |
|  66  | 42   | 102  |  B   | B                               |
|  67  | 43   | 103  |  C   | C                               |
|  68  | 44   | 104  |  D   | D                               |
|  69  | 45   | 105  |  E   | E                               |
|  70  | 46   | 106  |  F   | F                               |
|  71  | 47   | 107  |  G   | G                               |
|  72  | 48   | 110  |  H   | H                               |
|  73  | 49   | 111  |  I   | I                               |
|  74  | 4A   | 112  |  J   | J                               |
|  75  | 4B   | 113  |  K   | K                               |
|  76  | 4C   | 114  |  L   | L                               |
|  77  | 4D   | 115  |  M   | M                               |
|  78  | 4E   | 116  |  N   | N                               |
|  79  | 4F   | 117  |  O   | O                               |
|  80  | 50   | 120  |  P   | P                               |
|  81  | 51   | 121  |  Q   | Q                               |
|  82  | 52   | 122  |  R   | R                               |
|  83  | 53   | 123  |  S   | S                               |
|  84  | 54   | 124  |  T   | T                               |
|  85  | 55   | 125  |  U   | U                               |
|  86  | 56   | 126  |  V   | V                               |
|  87  | 57   | 127  |  W   | W                               |
|  88  | 58   | 130  |  X   | X                               |
|  89  | 59   | 131  |  Y   | Y                               |
|  90  | 5A   | 132  |  Z   | Z                               |
|  91  | 5B   | 133  |  [   | [                               |
|  92  | 5C   | 134  |  \   | \                               |
|  93  | 5D   | 135  |  ]   | ]                               |
|  94  | 5E   | 136  |  ^   | ^                               |
|  95  | 5F   | 137  |  _   | _                               |
|  96  | 60   | 140  |  `   | `                               |
|  97  | 61   | 141  |  a   | a                               |
|  98  | 62   | 142  |  b   | b                               |
|  99  | 63   | 143  |  c   | c                               |
| 100  | 64   | 144  |  d   | d                               |
| 101  | 65   | 145  |  e   | e                               |
| 102  | 66   | 146  |  f   | f                               |
| 103  | 67   | 147  |  g   | g                               |
| 104  | 68   | 150  |  h   | h                               |
| 105  | 69   | 151  |  i   | i                               |
| 106  | 6A   | 152  |  j   | j                               |
| 107  | 6B   | 153  |  k   | k                               |
| 108  | 6C   | 154  |  l   | l                               |
| 109  | 6D   | 155  |  m   | m                               |
| 110  | 6E   | 156  |  n   | n                               |
| 111  | 6F   | 157  |  o   | o                               |
| 112  | 70   | 160  |  p   | p                               |
| 113  | 71   | 161  |  q   | q                               |
| 114  | 72   | 162  |  r   | r                               |
| 115  | 73   | 163  |  s   | s                               |
| 116  | 74   | 164  |  t   | t                               |
| 117  | 75   | 165  |  u   | u                               |
| 118  | 76   | 166  |  v   | v                               |
| 119  | 77   | 167  |  w   | w                               |
| 120  | 78   | 170  |  x   | x                               |
| 121  | 79   | 171  |  y   | y                               |
| 122  | 7A   | 172  |  z   | z                               |
| 123  | 7B   | 173  |  {   | {                               |
| 124  | 7C   | 174  |  |   | |                               |
| 125  | 7D   | 175  |  }   | }                               |
| 126  | 7E   | 176  |  ~   | ~                               |
| 127  | 7F   | 177  |      | DEL (delete)                    |
+------+------+------+------+---------------------------------+

ascii_table.py

def print_ascii_table():
    # 詳細的ASCII描述字典,為控制字元和一些特殊字元提供簡潔的描述
    descriptions = {
        0: "NUL (null terminator)",  # 空字元,字串結束標誌
        1: "SOH (start of header)",  # 標題開始
        2: "STX (start of text)",  # 文字開始
        3: "ETX (end of text)",  # 文字結束
        4: "EOT (end of transmission)",  # 傳輸結束
        5: "ENQ (enquiry)",  # 請求
        6: "ACK (acknowledge)",  # 確認回應
        7: "BEL (bell, alert)",  # 響鈴
        8: "BS (backspace)",  # 退格
        9: "HT (horizontal tab)",  # 水平製表符
        10: "LF (line feed, new line)",  # 換行
        11: "VT (vertical tab)",  # 垂直製表符
        12: "FF (form feed, new page)",  # 換頁
        13: "CR (carriage return)",  # 回車
        14: "SO (shift out)",  # 鎖定釋放
        15: "SI (shift in)",  # 鎖定
        16: "DLE (data link escape)",  # 資料鏈路轉義
        17: "DC1 (device control 1, XON)",  # 裝置控制1
        18: "DC2 (device control 2)",  # 裝置控制2
        19: "DC3 (device control 3, XOFF)",  # 裝置控制3
        20: "DC4 (device control 4)",  # 裝置控制4
        21: "NAK (negative acknowledge)",  # 否認
        22: "SYN (synchronous idle)",  # 同步空閒
        23: "ETB (end of transmission block)",  # 傳輸塊結束
        24: "CAN (cancel)",  # 取消
        25: "EM (end of medium)",  # 媒介結束
        26: "SUB (substitute)",  # 替補
        27: "ESC (escape)",  # ESC
        28: "FS (file separator)",  # 檔案分隔符
        29: "GS (group separator)",  # 組分隔符
        30: "RS (record separator)",  # 記錄分隔符
        31: "US (unit separator)",  # 單元分隔符
        32: "Space (space character)",  # 空格
        127: "DEL (delete)"  # 刪除
        # 從33到126為所有可列印字元,這些描述被省略以簡化程式碼
    }

    # 列印表格頭部
    header = "| Dec  | Hex  | Oct  | Char | Description                     |"
    divider = "+" + "-" * 6 + "+" + "-" * 6 + "+" + "-" * 6 + "+" + "-" * 6 + "+" + "-" * 33 + "+"
    print(divider)
    print(header)
    print(divider)

    # 遍歷128個ASCII值,並列印每個字元的詳細資訊
    for i in range(128):
        char = chr(i)  # 將ASCII碼轉換為字元
        is_printable = char.isprintable()  # 檢查字元是否可列印。控制字元、空格、換行符等不可顯示字元將被視為不可列印。
        display_char = char if is_printable else ' '  # 如果可列印,則顯示字元;否則顯示空格
        desc = descriptions.get(i, display_char)  # 獲取描述或預設描述
        print(f"| {i:>3}  | {i:02X}   | {i:03o}  | {display_char:^4} | {desc:<31} |")
    print(divider)

print_ascii_table()

相關文章