求助,报错
file.py:#!coding=utf-8
#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:7}{0:^5}{0:>3}{0:^40}".format(titleList)) #打印标题
print(("{0:-^"+ str(len(titleList)) +"}{0:-^5}{0:->"+ str(len(titleList)) +"}{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 in ("-h", "--help"): #如果命令行参数中包含-h --help之类的词,说明用户要获得帮助说明
print("usage: {0} ".format(argv))
word = 0
else: #取得命令行参数并最小化
word = argv.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操作
执行:
Please input a string:f
decimal hex chr name
-------------------------------------------------------
40 28 ( Left Parenthesis
46 2E . Full Stop
52 34 4 Digit Four
53 35 5 Digit Five
70 46 F Latin Capital Letter F
91 5B [ Left Square Bracket
94 5E ^ Circumflex Accent
102 66 f Latin Small Letter F
123 7B { Left Curly Bracket
Traceback (most recent call last):
File "3.py", line 33, in <module>
File "3.py", line 16, in print_unicode
UnicodeEncodeError: 'ascii' codec can't encode character '\xaa' in position 13: ordinal not in range(128)
页:
[1]