Python之打印Unicode列表
2013-04-09 09:19
239 查看
近期在学习Python语言,为了巩固学习的质量,特设计一个程序作为强化练习。程序的需求是这样的,要实现如下图所示,根据用户输入unicode name检索并列出系统内置的unicode码表,需要运用到的库unicodedata,以及str.format()字符串格式化处理知识点。
(图1)
一、首先我们要在程序的头部引入相关的库:
我们先定义一个list用于存放标题:
然后把它以一定格式打印出来:
{0[0]:7} 表示取得format括号中第1个参数(表达式是从0开头的)的第一个列表值titleList[0],并最小长度为7,如果长度不够在右边用空格补齐,如果长度过长只取得前面7个。
{0[1]:^5} 与上面同理它取得第1个参数的第二个列表titleList[1],^表示居中
还有两个都是同理的,但要说明一下那几个特殊符号:
> 代表右对齐; < 代表左对齐; ^代表居中对齐
对于str.format()相关的操作我们可以用idle输入命令help(str.format)进行查看,更多深入说明可参照官方文档。
然后打印分割线:
打印unicode方法代码如下:
简单说明一下以上代码逻辑,循环判断目标code是否小于最大unicode值,如果小于就通过unicodedata.name()取得unicode name 并判断参数word是否为空或者 word是否被包含于unicode name 如果在就打印unicode行,通过一定的格式打印。
三、下面设计一个对接收用户输入参数的功能。有两种方式可以接收一个通过命令行参数,还有就是通过提示输入,并可以进行简单的帮助操作。代码如下:
整个程式的完整代码如下:
提示:程序在window Dos下执行会出现个别字符unicode编码错误,这是dos不能打印特殊的unicode字符,如需看到更多,建议用python的idle进行运行,但是也存在个别字符不能输出,相对要少
作者:Tandaly
地址:/article/9743538.html
(图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方法代码如下:
#打印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
地址:/article/9743538.html
相关文章推荐
- python打印列表中的内容,而不是unicode字符
- python处理utf8编码中文,及打印中文列表和字典
- python将dict中的unicode打印成中文
- Python(5)使用for...in循环和列表推导式打印一个99乘法表
- 解决Python3下打印utf-8字符串出现UnicodeEncodeError的问题
- python 用列表生成器 打印杨辉三角
- Python 打印list是显示unicode,转化成中文
- 一周stackoverflow热门问题选登:如何用Python for循环实现列表中数据两两循环打印?
- Python ——遍历列表(打印九九乘法表)
- python打印列表中指定元素的所有下标(5种方法)
- Python 中使用 for、while 循环打印杨辉三角练习(列表索引练习)。
- python 打印unicode字符串
- Python 打印嵌套list中每个数据(遍历列表)
- python语言打印含有中文的列表技巧
- 【整理】Python中实际上已经得到了正确的Unicode或某种编码的字符,但是看起来或打印出来却是乱码
- python print 打印unicode string的时候 去掉u' remove u'
- Python语言编写用多行Unicode字符串表示下面的唐诗’静夜思‘并打印
- 把CSV数据合成json样书存入字典、列表并打印的python脚本
- python练习题:循环打印嵌套列表
- Python2.7:打印字符串是Unicode