鱼C论坛

 找回密码
 立即注册
楼主: ckblt

[作品展示] 【Python】命令行表格渲染器

[复制链接]
发表于 2022-1-30 15:56:26 | 显示全部楼层
刚才那个遇到:
  1. list1 = [[-2,-121321423243434,0],[1342,2789879798798,3],[234,59789798,6]]
复制代码

时有点 Bug,所以我又改了:
  1. list1 = [[-2,-121321423243434,0],[1342,2789879798798,3],[234,59789798,6]]
  2. space = 0
  3. string = ""
  4. space_with_number = (len(str(max([each for each_row in list1 for each in each_row],key = lambda x:len(str(x)))))) * 2 + space
  5. space = []
  6. for i in range(len(list1)):

  7.     space.append([int(space_with_number - len(each)) for each in [each_row for each_row in (" ".join([str(each) for each in [each for each_row in list1 for each in each_row][len([each for each_row in list1[:i + 1] for each in each_row]) - len(list1[i]):len([each for each_row in list1[:i + 1] for each in each_row])]]).split())]])
  8. space = [each for each_line in space for each in each_line]
  9. value = [str(each) for each_line in list1 for each in each_line]
  10. spacer = (space_with_number - len(list1)) // (len(list1) - 1)
  11. i = 1
  12. for x in zip(value,space):
  13.     if (i - 1) % len(list1[0]) == 0:
  14.         print(spacer * "\n")
  15.     print(x[0] + x[1] * " ",end= "")
  16.     i += 1
  17. print()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2022-1-30 16:07:47 | 显示全部楼层
python爱好者. 发表于 2022-1-30 15:56
刚才那个遇到:

时有点 Bug,所以我又改了:

我做了个带有线的表格

  1. from typing import Union


  2. def no_list_list(
  3.     x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
  4. ) -> list:
  5.     """
  6.     禁止列表套娃函数(使用递归)
  7.     返回值: 列表

  8.     ### 参数:

  9.     x: 套娃的 列表 | 元组 | 集合
  10.     no_tuples: 禁止元组套娃
  11.     no_sets: 禁止集合套娃

  12.     ### 例子:

  13.     ```
  14.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
  15.     [1, 2, 3, 4, 5]

  16.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
  17.     [1, 2, 3, 4, 5, 6, 7]
  18.     ```
  19.     """

  20.     new_list = []

  21.     for i in x:
  22.         if (
  23.             (isinstance(i, list))
  24.             or (no_tuples and isinstance(i, tuple))
  25.             or (no_sets and isinstance(i, set))
  26.         ):
  27.             new_list.extend(no_list_list(i, no_tuples, no_sets))
  28.         else:
  29.             new_list.append(i)

  30.     return new_list


  31. class Table:
  32.     """
  33.     初始化 Table
  34.     """

  35.     def __init__(self, table: list):
  36.         self.table = table
  37.         self.length = len(self.table[0]), len(self.table)
  38.         self.__check()

  39.     """
  40.     检查 self.table 是否合法
  41.     """

  42.     def __check(self):
  43.         temp = set([len(x) for x in self.table])
  44.         if len(temp) != 1:
  45.             raise Exception()

  46.     """
  47.     渲染 self.table
  48.     """

  49.     def render(self):
  50.         col_spaces = 4
  51.         l = no_list_list(self.table)
  52.         rendered = ""

  53.         # 每列之间的空,存储到 col_spaces
  54.         for i in l:
  55.             if len(str(i)) > col_spaces - 4:
  56.                 col_spaces = len(str(i)) + 4

  57.         # 正式渲染
  58.         rendered += "-" * ((col_spaces + 1) * self.length[0] + 1) + "\n"
  59.         for i_i in range(len(self.table)):
  60.             i = self.table[i_i]
  61.             rendered += "|"
  62.             for j in i:
  63.                 rendered += str(j) + " " * (col_spaces - len(str(j))) + "|"

  64.             rendered += (
  65.                 "\n" + "-" * ((col_spaces + 1) * self.length[0] + 1) + "\n"
  66.             )

  67.         return rendered


  68. print(
  69.     Table(
  70.         [
  71.             [-2, -121321423243434, 0],
  72.             [1342, 2789879798798, 3],
  73.             [234, 59789798, 6],
  74.         ]
  75.     ).render()
  76. )
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-31 13:31:37 | 显示全部楼层
昨天没看到,今天中午才看到的:
  1. list1 = [[-2,-121321423243434,0],[1342,2789879798798,3],[234,59789798,6]]

  2. space = 0
  3. string = ""
  4. space_with_number = (len(str(max([each for each_row in list1 for each in each_row],key = lambda x:len(str(x)))))) * 2 + space
  5. space = []
  6. for i in range(len(list1)):

  7.     space.append([int(space_with_number - len(each)) for each in [each_row for each_row in (" ".join([str(each) for each in [each for each_row in list1 for each in each_row][len([each for each_row in list1[:i + 1] for each in each_row]) - len(list1[i]):len([each for each_row in list1[:i + 1] for each in each_row])]]).split())]])
  8. space = [each for each_line in space for each in each_line]
  9. value = [str(each) for each_line in list1 for each in each_line]
  10. spacer = (space_with_number - len(list1)) // (len(list1) - 1)
  11. print((len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2) * "-")
  12. mark_index = []
  13. length = sum(space[0:len(list1[0]) + 1]) + len("".join(value[0:len(list1[0]) + 1]))
  14. for i in range(0,length - 1,space[0] + len(value[0]) + 1):
  15.     mark_index.append(i)
  16. d = 1
  17. for x in zip(value,space):
  18.     if (d - 1) % len(list1[0]) == 0 and d > 1:
  19.         print()
  20.         for y in range(spacer - 2):
  21.             for i in range(len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2):
  22.                 print("|" if (i - 1) in mark_index else " ",end = "")
  23.             print()
  24.         print((len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2) * "-") #
  25.     if (d - 1) % len(list1[0]) == 0:
  26.         print(" |",end = "")
  27.     print(x[0] + (x[1] * " "),end = "|")
  28.     d += 1
  29. print()
  30. for y in range(spacer - 2):
  31.             for i in range(len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2):
  32.                 print("|" if (i - 1) in mark_index else " ",end = "")
  33.             print()
  34. print((len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2) * "-")


  35.    
复制代码

这个是打印正方形表格的代码,不是长方形了,你试试!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-1-31 14:14:33 | 显示全部楼层
我又写了个可以调节 4 个模式的打印代码:
  1. list1 = [[-2,-121321423243434,0],[1342,2789879798798,3],[234,59789798,6]]

  2. alpha = 0 #是否带图表#设置成功!
  3. square = 1 #是否是方形#还未设置!
  4. space = 0
  5. string = ""
  6. space_with_number = (len(str(max([each for each_row in list1 for each in each_row],key = lambda x:len(str(x)))))) * 2 + space
  7. space = []
  8. for i in range(len(list1)):

  9.     space.append([int(space_with_number - len(each)) for each in [each_row for each_row in (" ".join([str(each) for each in [each for each_row in list1 for each in each_row][len([each for each_row in list1[:i + 1] for each in each_row]) - len(list1[i]):len([each for each_row in list1[:i + 1] for each in each_row])]]).split())]])
  10. space = [each for each_line in space for each in each_line]
  11. value = [str(each) for each_line in list1 for each in each_line]
  12. spacer = (space_with_number - len(list1)) // (len(list1) - 1)
  13. if alpha == 1:
  14.     print((len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2) * "-")
  15. mark_index = []
  16. length = sum(space[0:len(list1[0]) + 1]) + len("".join(value[0:len(list1[0]) + 1]))
  17. for i in range(0,length - 1,space[0] + len(value[0]) + 1):
  18.     mark_index.append(i)
  19. d = 1
  20. for x in zip(value,space):
  21.     if (d - 1) % len(list1[0]) == 0 and d > 1 and square == 1:
  22.         print()
  23.         for y in range(spacer - 2):
  24.             for i in range(len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2):
  25.                 print("|" if ((i - 1) in mark_index) and (alpha == 1) else " ",end = "")
  26.             print()
  27.         if alpha == 1:
  28.             print((len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2) * "-")
  29.     if (d - 1) % len(list1[0]) == 0 and d > 1 and (not square == 1):
  30.         print()
  31.     if (d - 1) % len(list1[0]) == 0 and alpha == 1:
  32.         print(" |",end = "")
  33.     print(x[0] + (x[1] * " "),end = ("|" if alpha == 1 else ""))
  34.     d += 1
  35. print()
  36. if alpha == 1 and square == 1:
  37.     for y in range(spacer - 2):
  38.             for i in range(len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2):
  39.                 print("|" if (i - 1) in mark_index else " ",end = "")
  40.             print()
  41. if alpha == 1:
  42.     print((len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + len(list1[0]) + 1 + 2) * "-")


  43.    
  44. #尾部空格不对齐的原因是在设置打出时不带图表时,尾部便会对不齐,这是因为在选择不带图表打印的时候,将 "-" 和 "|" 和 " |" 替换成了与原来这些字符占格数不同的数量的空格!!!
  45. #不过这问题不大!
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-1-31 14:16:04 | 显示全部楼层
诶,不对,上面那个 square 变量后面的注释忘更新了,应该是:
  1. #是否是方形#设置成功!
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-1 10:43:28 | 显示全部楼层
ckblt 发表于 2022-1-30 16:07
我做了个带有线的表格

我又优化了一下,现在人能看懂了!:
  1. list1 = [[1,12,3],[4,5,6],[7,8,9]]

  2. alpha = 0 #是否带图表#设置成功!
  3. square = 1 #是否是方形#设置成功!
  4. ch = 0 #设置间距#可以是负数!因为它默认有预留空间!
  5. space_with_number = (len(str(max([each for each_row in list1 for each in each_row],key = lambda x:len(str(x)))))) * 2 + ch
  6. space = [(space_with_number - int(each)) for each in [len(str(each)) for each in [each for each_row in list1 for each in each_row]]]
  7. value = [str(each) for each_row in list1 for each in each_row] #列表中的每个值
  8. spacer = (((len(list1[0][:-1]) * space_with_number) + (len(str(list1[0][-1])))) - (len(list1))) // (len(list1) - 1) // 2 #计算每列每行之间的空隙
  9. length = len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + ((len(list1[0]) * len("|") + 1) if alpha == 1 else 0)
  10. top_or_bottle_words = (length + 2) * "-"
  11. mark_index = [i for i in range(1,length + 1,space_with_number + 1)]



  12. print(top_or_bottle_words) if alpha == 1 else "" #打印开头


  13. for i in range(len(value)): #打印中间部分
  14.     print() if i % len(list1[0]) == 0 and i > 1 else "" #如果换行的话
  15.         
  16.     if i % len(list1[0]) == 0 and i > 1 and square == 1: #如果换行要打印的格式是正方形时
  17.         for each_spacer in range(spacer):# -x
  18.             for each_index in range(length + 2):
  19.                 print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
  20.             print()
  21.               
  22.         print((length + 2) * "-") if alpha == 1 else "" #如果换行要打印的格式是有图表的格式时
  23.         
  24.     if i % len(list1[0]) == 0 and square != 1 and alpha == 1: #如果换行要打印的格式不是方形时   
  25.         print((length + 2) * "-") if i > 1 else "" #如果换行要打印的格式是有图表的格式时   
  26.         
  27.     if i % len(list1[0]) == 0 and alpha == 1: #如果换行要打印的格式是图表时
  28.         print(" |",end = "")
  29.         
  30.     print(value[i] + (space[i] * " "),end = ("|" if alpha == 1 else "")) #打印每行的值和空格
  31.    
  32.     print() if i == (len(value) - 1) else "" #如果是最后一次迭代时
  33.    
  34. if alpha == 1 and square == 1: #打印结尾剩余部分
  35.     for each_spacer in range(spacer):
  36.             for each_index in range(length + 2):
  37.                 print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
  38.             print()

  39.             
  40. print(top_or_bottle_words) if alpha == 1 else "" #打印结尾


  41.    
  42. #尾部空格不对齐的原因是在设置打出时不带图表时,尾部便会对不齐。
  43. #这是因为在选择不带图表打印的时候,将 "-" 和 "|" 和 " |" 替换成了与原来这些字符占格数不同的数量的空格!!!
  44. #不过这问题不大!
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-2-1 11:06:14 | 显示全部楼层
ckblt 发表于 2022-1-30 16:07
我做了个带有线的表格

你试试将 4 个功能像我这样合在一起?!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-1 11:07:52 | 显示全部楼层
python爱好者. 发表于 2022-2-1 11:06
你试试将 4 个功能像我这样合在一起?!

对了,你似乎还没有试过正方形的表格转换呢!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-1 11:08:27 | 显示全部楼层
python爱好者. 发表于 2022-2-1 11:06
你试试将 4 个功能像我这样合在一起?!

我需要亿点时间……
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-1 11:42:16 | 显示全部楼层
python爱好者. 发表于 2022-2-1 11:06
你试试将 4 个功能像我这样合在一起?!
  1. from typing import Union


  2. def no_list_list(
  3.     x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
  4. ) -> list:
  5.     """
  6.     禁止列表套娃函数(使用递归)
  7.     返回值: 列表

  8.     ### 参数:

  9.     x: 套娃的 列表 | 元组 | 集合
  10.     no_tuples: 禁止元组套娃
  11.     no_sets: 禁止集合套娃

  12.     ### 例子:

  13.     ```
  14.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
  15.     [1, 2, 3, 4, 5]

  16.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
  17.     [1, 2, 3, 4, 5, 6, 7]
  18.     ```
  19.     """

  20.     new_list = []

  21.     for i in x:
  22.         if (
  23.             (isinstance(i, list))
  24.             or (no_tuples and isinstance(i, tuple))
  25.             or (no_sets and isinstance(i, set))
  26.         ):
  27.             new_list.extend(no_list_list(i, no_tuples, no_sets))
  28.         else:
  29.             new_list.append(i)

  30.     return new_list


  31. class Table:
  32.     def __init__(self, table: list):
  33.         """
  34.         初始化 Table
  35.         """
  36.         self.table = table
  37.         self.length = len(self.table[0]), len(self.table)
  38.         self.__check()

  39.     def __check(self):
  40.         """
  41.         检查 self.table 是否合法
  42.         """
  43.         temp = set([len(x) for x in self.table])
  44.         if len(temp) != 1:
  45.             raise Exception()

  46.     def render(self, isChart=False, isSquare=False, spacing=4):
  47.         """
  48.         渲染 self.table

  49.         参数:
  50.         isChart: bool -- 外加线条
  51.         isSquare: bool -- 正方形模式
  52.         spacing: int -- 间距, 需要大于 0
  53.         """
  54.         col_spaces = spacing
  55.         l = no_list_list(self.table)
  56.         rendered = ""

  57.         # 每列之间的空,存储到 col_spaces
  58.         for i in l:
  59.             if len(str(i)) > col_spaces - spacing:
  60.                 col_spaces = len(str(i)) + spacing

  61.         # 正式渲染
  62.         if isChart:
  63.             rendered += "-" * ((col_spaces + 1) * self.length[0] + 1) + "\n"
  64.         for i_i in range(self.length[1]):
  65.             i = self.table[i_i]
  66.             if isChart:
  67.                 rendered += "|"
  68.             for j in i:
  69.                 rendered += str(j) + " " * (col_spaces - len(str(j)))
  70.                 if isChart:
  71.                     rendered += "|"
  72.             if isSquare:
  73.                 if isChart:
  74.                     rendered += "\n" + (
  75.                         ("|" + " " * col_spaces) * (self.length[0] + 1) + "\n"
  76.                     ) * (col_spaces // 3)
  77.                 elif i_i != self.length[1] - 1:
  78.                     rendered += "\n" * (col_spaces // 3)
  79.             else:
  80.                 rendered += "\n"
  81.             if isChart:
  82.                 rendered += "-" * ((col_spaces + 1) * self.length[0] + 1) + "\n"

  83.         return rendered


  84. print(
  85.     Table([[1, 2, 3], [4, 5, 6], [789, 123, 1], [12345, 11, 22]]).render(True, True, 4)
  86. )
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-1 11:59:47 | 显示全部楼层

这是将 4 个功能整合在一起的代码吗?
为什么我运行时修改参数它的结果却不变呢:
  1. list1 = [[1,12,3],[4,5,6],[7,8,9]]

  2. alpha = 0 #是否带图表#设置成功!
  3. square = 1 #是否是方形#设置成功!
  4. ch = 0 #设置间距#可以是负数!因为它默认有预留空间!
  5. space_with_number = (len(str(max([each for each_row in list1 for each in each_row],key = lambda x:len(str(x)))))) * 2 + ch
  6. space = [(space_with_number - int(each)) for each in [len(str(each)) for each in [each for each_row in list1 for each in each_row]]]
  7. value = [str(each) for each_row in list1 for each in each_row] #列表中的每个值
  8. spacer = (((len(list1[0][:-1]) * space_with_number) + (len(str(list1[0][-1])))) - (len(list1))) // (len(list1) - 1) // 2 #计算每列每行之间的空隙
  9. length = len("".join(value[:len(list1[0])])) + sum(space[:len(list1[0])]) + ((len(list1[0]) * len("|") + 1) if alpha == 1 else 0)
  10. top_or_bottle_words = (length + 2) * "-"
  11. mark_index = [i for i in range(1,length + 1,space_with_number + 1)]



  12. print(top_or_bottle_words) if alpha == 1 else "" #打印开头


  13. for i in range(len(value)): #打印中间部分
  14.     print() if i % len(list1[0]) == 0 and i > 1 else "" #如果换行的话
  15.         
  16.     if i % len(list1[0]) == 0 and i > 1 and square == 1: #如果换行要打印的格式是正方形时
  17.         for each_spacer in range(spacer):# -x
  18.             for each_index in range(length + 2):
  19.                 print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
  20.             print()
  21.               
  22.         print((length + 2) * "-") if alpha == 1 else "" #如果换行要打印的格式是有图表的格式时
  23.         
  24.     if i % len(list1[0]) == 0 and square != 1 and alpha == 1: #如果换行要打印的格式不是方形时   
  25.         print((length + 2) * "-") if i > 1 else "" #如果换行要打印的格式是有图表的格式时   
  26.         
  27.     if i % len(list1[0]) == 0 and alpha == 1: #如果换行要打印的格式是图表时
  28.         print(" |",end = "")
  29.         
  30.     print(value[i] + (space[i] * " "),end = ("|" if alpha == 1 else "")) #打印每行的值和空格
  31.    
  32.     print() if i == (len(value) - 1) else "" #如果是最后一次迭代时
  33.    
  34. if alpha == 1 and square == 1: #打印结尾剩余部分
  35.     for each_spacer in range(spacer):
  36.             for each_index in range(length + 2):
  37.                 print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
  38.             print()

  39.             
  40. print(top_or_bottle_words) if alpha == 1 else "" #打印结尾


  41.    
  42. #尾部空格不对齐的原因是在设置打出时不带图表时,尾部便会对不齐。
  43. #这是因为在选择不带图表打印的时候,将 "-" 和 "|" 和 " |" 替换成了与原来这些字符占格数不同的数量的空格!!!
  44. #不过这问题不大!



  45. from typing import Union


  46. def no_list_list(
  47.     x: Union[list, tuple, set], no_tuples: bool = False, no_sets: bool = False
  48. ) -> list:
  49.     """
  50.     禁止列表套娃函数(使用递归)
  51.     返回值: 列表

  52.     ### 参数:

  53.     x: 套娃的 列表 | 元组 | 集合
  54.     no_tuples: 禁止元组套娃
  55.     no_sets: 禁止集合套娃

  56.     ### 例子:

  57.     ```
  58.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
  59.     [1, 2, 3, 4, 5]

  60.     no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
  61.     [1, 2, 3, 4, 5, 6, 7]
  62.     ```
  63.     """

  64.     new_list = []

  65.     for i in x:
  66.         if (
  67.             (isinstance(i, list))
  68.             or (no_tuples and isinstance(i, tuple))
  69.             or (no_sets and isinstance(i, set))
  70.         ):
  71.             new_list.extend(no_list_list(i, no_tuples, no_sets))
  72.         else:
  73.             new_list.append(i)

  74.     return new_list


  75. class Table:
  76.     def __init__(self, table: list):
  77.         """
  78.         初始化 Table
  79.         """
  80.         self.table = table
  81.         self.length = len(self.table[0]), len(self.table)
  82.         self.__check()

  83.     def __check(self):
  84.         """
  85.         检查 self.table 是否合法
  86.         """
  87.         temp = set([len(x) for x in self.table])
  88.         if len(temp) != 1:
  89.             raise Exception()

  90.     def render(self, isChart=False, isSquare=False, spacing=4):#####这里 isChart 和 isSquare 改变后输出的结果却一直不变!
  91.         """
  92.         渲染 self.table

  93.         参数:
  94.         isChart: bool -- 外加线条
  95.         isSquare: bool -- 正方形模式
  96.         spacing: int -- 间距, 需要大于 0
  97.         """
  98.         col_spaces = spacing
  99.         l = no_list_list(self.table)
  100.         rendered = ""

  101.         # 每列之间的空,存储到 col_spaces
  102.         for i in l:
  103.             if len(str(i)) > col_spaces - spacing:
  104.                 col_spaces = len(str(i)) + spacing

  105.         # 正式渲染
  106.         if isChart:
  107.             rendered += "-" * ((col_spaces + 1) * self.length[0] + 1) + "\n"
  108.         for i_i in range(self.length[1]):
  109.             i = self.table[i_i]
  110.             if isChart:
  111.                 rendered += "|"
  112.             for j in i:
  113.                 rendered += str(j) + " " * (col_spaces - len(str(j)))
  114.                 if isChart:
  115.                     rendered += "|"
  116.             if isSquare:
  117.                 if isChart:
  118.                     rendered += "\n" + (
  119.                         ("|" + " " * col_spaces) * (self.length[0] + 1) + "\n"
  120.                     ) * (col_spaces // 3)
  121.                 elif i_i != self.length[1] - 1:
  122.                     rendered += "\n" * (col_spaces // 3)
  123.             else:
  124.                 rendered += "\n"
  125.             if isChart:
  126.                 rendered += "-" * ((col_spaces + 1) * self.length[0] + 1) + "\n"

  127.         return rendered


  128. print(Table(list1).render(True, True, 4))
  129. 上面是我的,下面是你的,结果明显不一样,你把参数再改一下,它的结果还是不会变:
  130. [code]1   12  3   
  131.               
  132. 4   5   6   
  133.               
  134. 7   8   9   
  135. ----------------------
  136. |1     |12    |3     |
  137. |      |      |      |      
  138. |      |      |      |      
  139. ----------------------
  140. |4     |5     |6     |
  141. |      |      |      |      
  142. |      |      |      |      
  143. ----------------------
  144. |7     |8     |9     |
  145. |      |      |      |      
  146. |      |      |      |      
  147. ----------------------
复制代码


还是说你还没合起来这些功能,这是你写的转换为正方形带表格的格式打印出来??
但是,不得不说,做的挺好的!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-1 12:01:59 | 显示全部楼层
  1. print(Table(list1).render(False, True, 4))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-1 12:02:34 | 显示全部楼层
python爱好者. 发表于 2022-2-1 11:59
这是将 4 个功能整合在一起的代码吗?
为什么我运行时修改参数它的结果却不变呢:
  1. print(Table(list1).render(False, True, 4))
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2022-2-1 12:06:53 | 显示全部楼层

原来如此,是我的食用方法错了,抱歉!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-1 12:09:47 | 显示全部楼层
但是这样条件下打出来的似乎不像正方形啊:
  1. list1 = [[1,897977912,3],[4,5,6],[7,8,9],[213213,213213,213],[14,214,124]]

  2. print(Table(list1).render(1, 1, 4))
复制代码

结果是:
  1. -------------------------------------------
  2. |1            |897977912    |3            |
  3. |             |             |             |            
  4. |             |             |             |            
  5. |             |             |             |            
  6. |             |             |             |            
  7. -------------------------------------------
  8. |4            |5            |6            |
  9. |             |             |             |            
  10. |             |             |             |            
  11. |             |             |             |            
  12. |             |             |             |            
  13. -------------------------------------------
  14. |7            |8            |9            |
  15. |             |             |             |            
  16. |             |             |             |            
  17. |             |             |             |            
  18. |             |             |             |            
  19. -------------------------------------------
  20. |213213       |213213       |213          |
  21. |             |             |             |            
  22. |             |             |             |            
  23. |             |             |             |            
  24. |             |             |             |            
  25. -------------------------------------------
  26. |14           |214          |124          |
  27. |             |             |             |            
  28. |             |             |             |            
  29. |             |             |             |            
  30. |             |             |             |            
  31. -------------------------------------------
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-1 12:10:18 | 显示全部楼层
长是不是有点太长了?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-1 12:10:48 | 显示全部楼层

长是不是有点太长了?!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-2-1 12:13:03 | 显示全部楼层
python爱好者. 发表于 2022-2-1 12:09
但是这样条件下打出来的似乎不像正方形啊:

结果是:

可能是字体不一样
Snipaste_2022-02-01_12-11-22.png
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-1 12:13:41 | 显示全部楼层
那我的为什么可以?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-2-1 12:16:17 | 显示全部楼层
Python 的两行的宽度的视觉效果是只等于一列的宽度,所以我在我的代码里用了 "\\ 2",
但是你算一下它的长和宽相等吗?所以应该和这个无关吧!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-30 09:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表