标题: [原创代码] 学习python,自己写的第一个对象! [打印本页]
作者: Gin_Q 时间: 2020-5-13 00:15 标题: 学习python,自己写的第一个对象!
- #coding = utf-8
- #Rev.01
- class printf_:
- def __init__(self):
- #表格样式
- self.__form = '-'
- self.__form_end = '+'
- self.__form_cnter = '| {}{} '
- self.__form_cnter_end = self.__form_cnter + '|'
-
- def __printf_head(self,string_len,head=1):
- self.__str_len = string_len
- self.__form = '-'
- #字符串处理‘+----’,+2多出的空格
- self.__form *= (self.__str_len+2)
- self.__form = list(self.__form)
- self.__form.insert(0,'+')
- self.__form = ''.join(self.__form)
- self.__head = head
- if self.__head == 1:
- print(self.__form,end='')
- else:
- self.__form_end = self.__form + '+'
- print(self.__form_end)
- def __printf_cnter(self,string,filling_len,cnter=1):
- self.__filling_len = filling_len
- self.__str = string
- self.__cnter = cnter
- if self.__cnter == 1:
- print(self.__form_cnter.format(self.__str,' '*self.__filling_len),end='')
- else:
- print(self.__form_cnter_end.format(self.__str,' '*self.__filling_len))
-
-
- def __handling_str(self,string,form_len):
- #标题长度大于等于规定所占字节,截断
- #小于标题长度:求出剩余字节数,(filling_name)空格填充
- #用于存储字符串长度
- self.__string = string
- self.__form_len = form_len
- self.__str_len = len(self.__string)
- self.__filling = 0
- if self.__str_len >= self.__form_len:
- self.__string = self.__string[:self.__form_len]
- else:
- self.__filling = self.__form_len - self.__str_len
- return [self.__string,self.__filling]
-
- def printf_cnter(self,*data):
- self.__indexa = 0
- self.__indexb = 1
- self.__temp = []
- self.__dictargv = data
- self.__dictargv_len = len(self.__dictargv)
-
- while self.__indexb <= self.__dictargv_len:
- if self.__indexb >= self.__dictargv_len-1:
- self.__temp = self.__handling_str(str(self.__dictargv[self.__indexa]),self.__dictargv[self.__indexb])
- self.__printf_cnter(self.__temp[0],self.__temp[1],0)
- break
- self.__temp = self.__handling_str(str(self.__dictargv[self.__indexa]),self.__dictargv[self.__indexb])
- self.__printf_cnter(self.__temp[0],self.__temp[1])
- self.__indexb += 1
- self.__indexa = self.__indexb
- self.__indexb += 1
-
- def printf_head(self,*data):
- self.__temp = []
- self.__dictargv = data
- self.__dictargv_len = len(self.__dictargv)
- self.__count = 0
- for self.__i in self.__dictargv:
- self.__count += 1
- if self.__count == self.__dictargv_len:
- self.__printf_head(self.__i,0)
- break
- self.__printf_head(self.__i)
复制代码
- #coding = utf-8
- from printf_ import printf_
-
- f = printf_()
-
- f.printf_head(2,2)
- f.printf_cnter(123,2,986,2)
- f.printf_head(2,2)
-
- dict_data = dict(li=69,Gin=100,LU=93,mike=66,huazai=86)
- num = 4
- f.printf_head(num,num)
- f.printf_cnter('name',num,'score',num)
- f.printf_head(num,num)
- for i,j in dict_data.items():
- f.printf_cnter(i,num,j,num)
- f.printf_head(num,num)
-
- num = 6
- list_data = [['li',69,'Boy'],['Gin',100,'Boy'],['LU',93,'Girl'],['mike',66,'Boy'],['huazai',86,'Boy']]
- f.printf_head(num,num,num)
- f.printf_cnter('name',num,'score',num,'Sex',num)
- f.printf_head(num,num,num)
- for i in list_data:
- f.printf_cnter(i[0],num,i[1],num,i[2],num)
- f.printf_head(num,num,num)
复制代码
- +----+----+
- | 12 | 98 |
- +----+----+
- +------+------+
- | name | scor |
- +------+------+
- | li | 69 |
- | Gin | 100 |
- | LU | 93 |
- | mike | 66 |
- | huaz | 86 |
- +------+------+
- +--------+--------+--------+
- | name | score | Sex |
- +--------+--------+--------+
- | li | 69 | Boy |
- | Gin | 100 | Boy |
- | LU | 93 | Girl |
- | mike | 66 | Boy |
- | huazai | 86 | Boy |
- +--------+--------+--------+
-
- 请按任意键继续. . .
复制代码
作者: Gin_Q 时间: 2020-5-13 00:30
本帖最后由 Gin_Q 于 2020-5-13 00:32 编辑
使用以下方法,可以更简单
print('{:.10s}'.format(str(123.1321)))
作者: wujunkai 时间: 2020-5-13 06:16
好吧,加油(ง •̀_•́)ง
作者: netdzb 时间: 2020-5-13 08:46
回复 1# Gin_Q
我也跟着一起学习了。
作者: Gin_Q 时间: 2020-5-13 12:41
加油!加油 (ง •̀o•́)ง (ง •̀o•́)ง
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |