近期在學習Python語言,爲了鞏固學習的質量,特設計一個程序作爲強化練習。程序的需求是這樣的,要實現如下圖所示,根據用戶輸入unicode name檢索並列出系統內置的unicode碼錶,需要運用到的庫unicodedata,以及str.format()字符串格式化處理知識點。
(圖1)
一、首先我們要在程序的頭部引入相關的庫:
import sys
import unicodedata
二、定義一個方法根據傳入的unicode name實現unicode列表的打印,需要說明的是python語言定義方法的格式如下:
def function_name(arg): #方法頭以及形參
pass #方法體
爲了實現如(圖1)那樣的效果,我們首先需要打印列表頭以及分割線。
我們先定義一個list用於存放標題:
titleList = ["decimal", "hex", "chr", "name"]
然後把它以一定格式打印出來:
print("{0[0]:7}{0[1]:^5}{0[2]:>3}{0[3]:^40}".format(titleList))
到這裏,運用到了str.format()這個方法對字符串進行格式化操作,簡單的說明一下:
{0[0]:7} 表示取得format括號中第1個參數(表達式是從0開頭的)的第一個列表值titleList[0],並最小長度爲7,如果長度不夠在右邊用空格補齊,如果長度過長只取得前面7個。
{0[1]:^5} 與上面同理它取得第1個參數的第二個列表titleList[1],^表示居中
還有兩個都是同理的,但要說明一下那幾個特殊符號:
> 代表右對齊; < 代表左對齊; ^代表居中對齊
對於str.format()相關的操作我們可以用idle輸入命令help(str.format)進行查看,更多深入說明可參照官方文檔。
然後打印分割線:
print(("{0:-^"+ str(len(titleList[0])) +"}{0:-^5}{0:->"+ str(len(titleList[2])) +"}{0:-^40}").format(""))
#打印unicode
def print_unicode(word):
titleList = ["decimal", "hex", "chr", "name"] #標題列表
print("{0[0]:7}{0[1]:^5}{0[2]:>3}{0[3]:^40}".format(titleList)) #打印標題
print(("{0:-^"+ str(len(titleList[0])) +"}{0:-^5}{0:->"+ str(len(titleList[2])) +"}{0:-^40}").format("")) #打印分隔線
code = ord(" ") #decimal 將空格轉換爲十進制
end = sys.maxunicode #取得系統內置最大unicode編碼的十進制
while code < end: #循環判斷只要code小於最大unicode就執行操作
name = unicodedata.name(chr(code), "***unknown***") #根據unicode字符取得對應的unicode name
if word is None or word in name.lower(): #如果word爲None 或者word被包含於name中
print("{0:>7}{0:>5X}{0:^3c}{1:<40}".format(code, name.title())) #打印unicode列表
code += 1 #code增1
簡單說明一下以上代碼邏輯,循環判斷目標code是否小於最大unicode值,如果小於就通過unicodedata.name()取得unicode name 並判斷參數word是否爲空或者 word是否被包含於unicode name 如果在就打印unicode行,通過一定的格式打印。
三、下面設計一個對接收用戶輸入參數的功能。有兩種方式可以接收一個通過命令行參數,還有就是通過提示輸入,並可以進行簡單的幫助操作。代碼如下:
if __name__ == "__main__":
word = None
argv = sys.argv
if len(argv) > 1: #用戶輸入了命令行參數
if argv[1] in ("-h", "--help"): #如果命令行參數中包含-h --help之類的詞,說明用戶要獲得幫助說明
print("usage: {0} [string]".format(argv[0]))
word = 0
else: #取得命令行參數並最小化
word = argv[1].lower()
else: #命令行沒有輸入任何參數,就需要提示並讓用戶輸入參數
line = input("Please input a string:") #提示輸入參數
if line is not None: #如果參數不爲空
word = line.lower() #參數進行最小化
if word != 0: #如果word不爲0,則是需要進行打印unicode操作
print_unicode(word) #調用定義的打印unicode操作
整個程式的完整代碼如下:
#Author Tandaly
#Date 2013-04-08
#File PrintUnicode.py
#引入相關包
import sys
import unicodedata
#打印unicode
def print_unicode(word):
titleList = ["decimal", "hex", "chr", "name"] #標題列表
print("{0[0]:7}{0[1]:^5}{0[2]:>3}{0[3]:^40}".format(titleList)) #打印標題
print(("{0:-^"+ str(len(titleList[0])) +"}{0:-^5}{0:->"+ str(len(titleList[2])) +"}{0:-^40}").format("")) #打印分隔線
code = ord(" ") #decimal 將空格轉換爲十進制
end = sys.maxunicode #取得系統內置最大unicode編碼的十進制
while code < end: #循環判斷只要code小於最大unicode就執行操作
name = unicodedata.name(chr(code), "***unknown***") #根據unicode字符取得對應的unicode name
if word is None or word in name.lower(): #如果word爲None 或者word被包含於name中
print("{0:>7}{0:>5X}{0:^3c}{1:<40}".format(code, name.title())) #打印unicode列表
code += 1 #code增1
if __name__ == "__main__":
word = None
argv = sys.argv
if len(argv) > 1: #用戶輸入了命令行參數
if argv[1] in ("-h", "--help"): #如果命令行參數中包含-h --help之類的詞,說明用戶要獲得幫助說明
print("usage: {0} [string]".format(argv[0]))
word = 0
else: #取得命令行參數並最小化
word = argv[1].lower()
else: #命令行沒有輸入任何參數,就需要提示並讓用戶輸入參數
line = input("Please input a string:") #提示輸入參數
if line is not None: #如果參數不爲空
word = line.lower() #參數進行最小化
if word != 0: #如果word不爲0,則是需要進行打印unicode操作
print_unicode(word) #調用定義的打印unicode操作
提示:程序在window Dos下執行會出現個別字符unicode編碼錯誤,這是dos不能打印特殊的unicode字符,如需看到更多,建議用python的idle進行運行,但是也存在個別字符不能輸出,相對要少
作者:Tandaly
地址:http://blog.csdn.net/tandaly/article/details/8775820