[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创代码] 学习python,自己写的第一个对象!

  1. #coding = utf-8
  2. #Rev.01
  3. class printf_:
  4.     def __init__(self):
  5.         #表格样式
  6.         self.__form = '-'
  7.         self.__form_end = '+'
  8.         self.__form_cnter = '| {}{} '
  9.         self.__form_cnter_end = self.__form_cnter + '|'
  10.    
  11.     def __printf_head(self,string_len,head=1):
  12.         self.__str_len = string_len
  13.         self.__form = '-'
  14.         #字符串处理‘+----’,+2多出的空格
  15.         self.__form *= (self.__str_len+2)
  16.         self.__form = list(self.__form)
  17.         self.__form.insert(0,'+')
  18.         self.__form = ''.join(self.__form)
  19.         self.__head = head
  20.         if self.__head == 1:
  21.             print(self.__form,end='')
  22.         else:
  23.             self.__form_end = self.__form + '+'
  24.             print(self.__form_end)
  25.     def __printf_cnter(self,string,filling_len,cnter=1):
  26.         self.__filling_len = filling_len
  27.         self.__str = string
  28.         self.__cnter = cnter
  29.         if self.__cnter == 1:
  30.             print(self.__form_cnter.format(self.__str,' '*self.__filling_len),end='')
  31.         else:
  32.             print(self.__form_cnter_end.format(self.__str,' '*self.__filling_len))
  33.     def __handling_str(self,string,form_len):
  34.         #标题长度大于等于规定所占字节,截断
  35.         #小于标题长度:求出剩余字节数,(filling_name)空格填充
  36.         #用于存储字符串长度
  37.         self.__string = string
  38.         self.__form_len = form_len
  39.         self.__str_len = len(self.__string)
  40.         self.__filling = 0
  41.         if self.__str_len >= self.__form_len:
  42.             self.__string = self.__string[:self.__form_len]
  43.         else:
  44.             self.__filling = self.__form_len - self.__str_len
  45.         return [self.__string,self.__filling]
  46.         
  47.     def printf_cnter(self,*data):
  48.         self.__indexa = 0
  49.         self.__indexb = 1
  50.         self.__temp = []
  51.         self.__dictargv = data
  52.         self.__dictargv_len = len(self.__dictargv)
  53.         
  54.         while self.__indexb <= self.__dictargv_len:
  55.             if self.__indexb >= self.__dictargv_len-1:
  56.                 self.__temp = self.__handling_str(str(self.__dictargv[self.__indexa]),self.__dictargv[self.__indexb])
  57.                 self.__printf_cnter(self.__temp[0],self.__temp[1],0)
  58.                 break
  59.             self.__temp = self.__handling_str(str(self.__dictargv[self.__indexa]),self.__dictargv[self.__indexb])
  60.             self.__printf_cnter(self.__temp[0],self.__temp[1])
  61.             self.__indexb += 1
  62.             self.__indexa = self.__indexb
  63.             self.__indexb += 1
  64.    
  65.     def printf_head(self,*data):
  66.         self.__temp = []
  67.         self.__dictargv = data
  68.         self.__dictargv_len = len(self.__dictargv)
  69.         self.__count = 0
  70.         for self.__i in self.__dictargv:
  71.             self.__count += 1
  72.             if self.__count == self.__dictargv_len:
  73.                 self.__printf_head(self.__i,0)
  74.                 break
  75.             self.__printf_head(self.__i)
复制代码
  1. #coding = utf-8
  2. from printf_ import printf_
  3. f = printf_()
  4. f.printf_head(2,2)
  5. f.printf_cnter(123,2,986,2)
  6. f.printf_head(2,2)
  7. dict_data = dict(li=69,Gin=100,LU=93,mike=66,huazai=86)
  8. num = 4
  9. f.printf_head(num,num)
  10. f.printf_cnter('name',num,'score',num)
  11. f.printf_head(num,num)
  12. for i,j in dict_data.items():
  13.     f.printf_cnter(i,num,j,num)
  14. f.printf_head(num,num)
  15. num = 6
  16. list_data = [['li',69,'Boy'],['Gin',100,'Boy'],['LU',93,'Girl'],['mike',66,'Boy'],['huazai',86,'Boy']]
  17. f.printf_head(num,num,num)
  18. f.printf_cnter('name',num,'score',num,'Sex',num)
  19. f.printf_head(num,num,num)
  20. for i in list_data:
  21.     f.printf_cnter(i[0],num,i[1],num,i[2],num)
  22. f.printf_head(num,num,num)
复制代码
  1. +----+----+
  2. | 12 | 98 |
  3. +----+----+
  4. +------+------+
  5. | name | scor |
  6. +------+------+
  7. | li   | 69   |
  8. | Gin  | 100  |
  9. | LU   | 93   |
  10. | mike | 66   |
  11. | huaz | 86   |
  12. +------+------+
  13. +--------+--------+--------+
  14. | name   | score  | Sex    |
  15. +--------+--------+--------+
  16. | li     | 69     | Boy    |
  17. | Gin    | 100    | Boy    |
  18. | LU     | 93     | Girl   |
  19. | mike   | 66     | Boy    |
  20. | huazai | 86     | Boy    |
  21. +--------+--------+--------+
  22. 请按任意键继续. . .
复制代码

本帖最后由 Gin_Q 于 2020-5-13 00:32 编辑

使用以下方法,可以更简单
print('{:.10s}'.format(str(123.1321)))

TOP

好吧,加油(ง •̀_•́)ง

TOP

回复 1# Gin_Q

我也跟着一起学习了。

TOP

加油!加油 (ง •̀o•́)ง (ง •̀o•́)ง

TOP

返回列表