Python之打印Unicode列表

来源:互联网 发布:淘宝网中老年运动衣 编辑:程序博客网 时间:2024/05/17 22:57

       近期在学习Python语言,为了巩固学习的质量,特设计一个程序作为强化练习。程序的需求是这样的,要实现如下图所示,根据用户输入unicode name检索并列出系统内置的unicode码表,需要运用到的库unicodedata,以及str.format()字符串格式化处理知识点。


(图1)

一、首先我们要在程序的头部引入相关的库:

import sysimport 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方法代码如下:

#打印unicodedef 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 nameif 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 = Noneargv = sys.argvif len(argv) > 1:#用户输入了命令行参数if argv[1] in ("-h", "--help"):#如果命令行参数中包含-h --help之类的词,说明用户要获得帮助说明print("usage: {0} [string]".format(argv[0]))word = 0else:#取得命令行参数并最小化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 sysimport unicodedata#打印unicodedef 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 nameif 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增1if __name__ == "__main__":word = Noneargv = sys.argvif len(argv) > 1:#用户输入了命令行参数if argv[1] in ("-h", "--help"):#如果命令行参数中包含-h --help之类的词,说明用户要获得帮助说明print("usage: {0} [string]".format(argv[0]))word = 0else:#取得命令行参数并最小化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



原创粉丝点击