python爱好者. 发表于 2022-1-30 15:56:26

刚才那个遇到:
list1 = [[-2,-121321423243434,0],,]
时有点 Bug,所以我又改了:
list1 = [[-2,-121321423243434,0],,]
space = 0
string = ""
space_with_number = (len(str(max(,key = lambda x:len(str(x)))))) * 2 + space
space = []
for i in range(len(list1)):

    space.append( for each in each_row]) - len(list1):len( for each in each_row])]]).split())]])
space =
value =
spacer = (space_with_number - len(list1)) // (len(list1) - 1)
i = 1
for x in zip(value,space):
    if (i - 1) % len(list1) == 0:
      print(spacer * "\n")
    print(x + x * " ",end= "")
    i += 1
print()

ckblt 发表于 2022-1-30 16:07:47

python爱好者. 发表于 2022-1-30 15:56
刚才那个遇到:

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

我做了个带有线的表格

from typing import Union


def no_list_list(
    x: Union, no_tuples: bool = False, no_sets: bool = False
) -> list:
    """
    禁止列表套娃函数(使用递归)
    返回值: 列表

    ### 参数:

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

    ### 例子:

    ```
    no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
   

    no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
   
    ```
    """

    new_list = []

    for i in x:
      if (
            (isinstance(i, list))
            or (no_tuples and isinstance(i, tuple))
            or (no_sets and isinstance(i, set))
      ):
            new_list.extend(no_list_list(i, no_tuples, no_sets))
      else:
            new_list.append(i)

    return new_list


class Table:
    """
    初始化 Table
    """

    def __init__(self, table: list):
      self.table = table
      self.length = len(self.table), len(self.table)
      self.__check()

    """
    检查 self.table 是否合法
    """

    def __check(self):
      temp = set()
      if len(temp) != 1:
            raise Exception()

    """
    渲染 self.table
    """

    def render(self):
      col_spaces = 4
      l = no_list_list(self.table)
      rendered = ""

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

      # 正式渲染
      rendered += "-" * ((col_spaces + 1) * self.length + 1) + "\n"
      for i_i in range(len(self.table)):
            i = self.table
            rendered += "|"
            for j in i:
                rendered += str(j) + " " * (col_spaces - len(str(j))) + "|"

            rendered += (
                "\n" + "-" * ((col_spaces + 1) * self.length + 1) + "\n"
            )

      return rendered


print(
    Table(
      [
            [-2, -121321423243434, 0],
            ,
            ,
      ]
    ).render()
)

python爱好者. 发表于 2022-1-31 13:31:37

昨天没看到,今天中午才看到的:
list1 = [[-2,-121321423243434,0],,]

space = 0
string = ""
space_with_number = (len(str(max(,key = lambda x:len(str(x)))))) * 2 + space
space = []
for i in range(len(list1)):

    space.append( for each in each_row]) - len(list1):len( for each in each_row])]]).split())]])
space =
value =
spacer = (space_with_number - len(list1)) // (len(list1) - 1)
print((len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2) * "-")
mark_index = []
length = sum(space) + 1]) + len("".join(value) + 1]))
for i in range(0,length - 1,space + len(value) + 1):
    mark_index.append(i)
d = 1
for x in zip(value,space):
    if (d - 1) % len(list1) == 0 and d > 1:
      print()
      for y in range(spacer - 2):
            for i in range(len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2):
                print("|" if (i - 1) in mark_index else " ",end = "")
            print()
      print((len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2) * "-") #
    if (d - 1) % len(list1) == 0:
      print(" |",end = "")
    print(x + (x * " "),end = "|")
    d += 1
print()
for y in range(spacer - 2):
            for i in range(len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2):
                print("|" if (i - 1) in mark_index else " ",end = "")
            print()
print((len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2) * "-")


   
这个是打印正方形表格的代码,不是长方形了,你试试!!!{:10_256:}

python爱好者. 发表于 2022-1-31 14:14:33

我又写了个可以调节 4 个模式的打印代码:
list1 = [[-2,-121321423243434,0],,]

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

    space.append( for each in each_row]) - len(list1):len( for each in each_row])]]).split())]])
space =
value =
spacer = (space_with_number - len(list1)) // (len(list1) - 1)
if alpha == 1:
    print((len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2) * "-")
mark_index = []
length = sum(space) + 1]) + len("".join(value) + 1]))
for i in range(0,length - 1,space + len(value) + 1):
    mark_index.append(i)
d = 1
for x in zip(value,space):
    if (d - 1) % len(list1) == 0 and d > 1 and square == 1:
      print()
      for y in range(spacer - 2):
            for i in range(len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2):
                print("|" if ((i - 1) in mark_index) and (alpha == 1) else " ",end = "")
            print()
      if alpha == 1:
            print((len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2) * "-")
    if (d - 1) % len(list1) == 0 and d > 1 and (not square == 1):
      print()
    if (d - 1) % len(list1) == 0 and alpha == 1:
      print(" |",end = "")
    print(x + (x * " "),end = ("|" if alpha == 1 else ""))
    d += 1
print()
if alpha == 1 and square == 1:
    for y in range(spacer - 2):
            for i in range(len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2):
                print("|" if (i - 1) in mark_index else " ",end = "")
            print()
if alpha == 1:
    print((len("".join(value[:len(list1)])) + sum(space[:len(list1)]) + len(list1) + 1 + 2) * "-")


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

python爱好者. 发表于 2022-1-31 14:16:04

诶,不对,上面那个 square 变量后面的注释忘更新了,应该是:
#是否是方形#设置成功!

python爱好者. 发表于 2022-2-1 10:43:28

ckblt 发表于 2022-1-30 16:07
我做了个带有线的表格

我又优化了一下,现在人能看懂了!:
list1 = [,,]

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



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


for i in range(len(value)): #打印中间部分
    print() if i % len(list1) == 0 and i > 1 else "" #如果换行的话
      
    if i % len(list1) == 0 and i > 1 and square == 1: #如果换行要打印的格式是正方形时
      for each_spacer in range(spacer):# -x
            for each_index in range(length + 2):
                print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
            print()
            
      print((length + 2) * "-") if alpha == 1 else "" #如果换行要打印的格式是有图表的格式时
      
    if i % len(list1) == 0 and square != 1 and alpha == 1: #如果换行要打印的格式不是方形时   
      print((length + 2) * "-") if i > 1 else "" #如果换行要打印的格式是有图表的格式时   
      
    if i % len(list1) == 0 and alpha == 1: #如果换行要打印的格式是图表时
      print(" |",end = "")
      
    print(value + (space * " "),end = ("|" if alpha == 1 else "")) #打印每行的值和空格
   
    print() if i == (len(value) - 1) else "" #如果是最后一次迭代时
   
if alpha == 1 and square == 1: #打印结尾剩余部分
    for each_spacer in range(spacer):
            for each_index in range(length + 2):
                print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
            print()

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


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

python爱好者. 发表于 2022-2-1 11:06:14

ckblt 发表于 2022-1-30 16:07
我做了个带有线的表格

你试试将 4 个功能像我这样合在一起?!{:10_299:}

python爱好者. 发表于 2022-2-1 11:07:52

python爱好者. 发表于 2022-2-1 11:06
你试试将 4 个功能像我这样合在一起?!

对了,你似乎还没有试过正方形的表格转换呢!!!

ckblt 发表于 2022-2-1 11:08:27

python爱好者. 发表于 2022-2-1 11:06
你试试将 4 个功能像我这样合在一起?!

我需要亿点时间……

ckblt 发表于 2022-2-1 11:42:16

python爱好者. 发表于 2022-2-1 11:06
你试试将 4 个功能像我这样合在一起?!

from typing import Union


def no_list_list(
    x: Union, no_tuples: bool = False, no_sets: bool = False
) -> list:
    """
    禁止列表套娃函数(使用递归)
    返回值: 列表

    ### 参数:

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

    ### 例子:

    ```
    no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
   

    no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
   
    ```
    """

    new_list = []

    for i in x:
      if (
            (isinstance(i, list))
            or (no_tuples and isinstance(i, tuple))
            or (no_sets and isinstance(i, set))
      ):
            new_list.extend(no_list_list(i, no_tuples, no_sets))
      else:
            new_list.append(i)

    return new_list


class Table:
    def __init__(self, table: list):
      """
      初始化 Table
      """
      self.table = table
      self.length = len(self.table), len(self.table)
      self.__check()

    def __check(self):
      """
      检查 self.table 是否合法
      """
      temp = set()
      if len(temp) != 1:
            raise Exception()

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

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

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

      # 正式渲染
      if isChart:
            rendered += "-" * ((col_spaces + 1) * self.length + 1) + "\n"
      for i_i in range(self.length):
            i = self.table
            if isChart:
                rendered += "|"
            for j in i:
                rendered += str(j) + " " * (col_spaces - len(str(j)))
                if isChart:
                  rendered += "|"
            if isSquare:
                if isChart:
                  rendered += "\n" + (
                        ("|" + " " * col_spaces) * (self.length + 1) + "\n"
                  ) * (col_spaces // 3)
                elif i_i != self.length - 1:
                  rendered += "\n" * (col_spaces // 3)
            else:
                rendered += "\n"
            if isChart:
                rendered += "-" * ((col_spaces + 1) * self.length + 1) + "\n"

      return rendered


print(
    Table([, , , ]).render(True, True, 4)
)

python爱好者. 发表于 2022-2-1 11:59:47

ckblt 发表于 2022-2-1 11:42


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

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



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


for i in range(len(value)): #打印中间部分
    print() if i % len(list1) == 0 and i > 1 else "" #如果换行的话
      
    if i % len(list1) == 0 and i > 1 and square == 1: #如果换行要打印的格式是正方形时
      for each_spacer in range(spacer):# -x
            for each_index in range(length + 2):
                print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
            print()
            
      print((length + 2) * "-") if alpha == 1 else "" #如果换行要打印的格式是有图表的格式时
      
    if i % len(list1) == 0 and square != 1 and alpha == 1: #如果换行要打印的格式不是方形时   
      print((length + 2) * "-") if i > 1 else "" #如果换行要打印的格式是有图表的格式时   
      
    if i % len(list1) == 0 and alpha == 1: #如果换行要打印的格式是图表时
      print(" |",end = "")
      
    print(value + (space * " "),end = ("|" if alpha == 1 else "")) #打印每行的值和空格
   
    print() if i == (len(value) - 1) else "" #如果是最后一次迭代时
   
if alpha == 1 and square == 1: #打印结尾剩余部分
    for each_spacer in range(spacer):
            for each_index in range(length + 2):
                print("|" if (each_index in mark_index) and (alpha == 1) else " ",end = "")
            print()

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


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



from typing import Union


def no_list_list(
    x: Union, no_tuples: bool = False, no_sets: bool = False
) -> list:
    """
    禁止列表套娃函数(使用递归)
    返回值: 列表

    ### 参数:

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

    ### 例子:

    ```
    no_list_list( [ [ [ 1, 2, 3, 4, 5 ] ] ] )
   

    no_list_list( [ [ [ 1, 2, 3, 4, 5 ], 6 ], 7 ] )
   
    ```
    """

    new_list = []

    for i in x:
      if (
            (isinstance(i, list))
            or (no_tuples and isinstance(i, tuple))
            or (no_sets and isinstance(i, set))
      ):
            new_list.extend(no_list_list(i, no_tuples, no_sets))
      else:
            new_list.append(i)

    return new_list


class Table:
    def __init__(self, table: list):
      """
      初始化 Table
      """
      self.table = table
      self.length = len(self.table), len(self.table)
      self.__check()

    def __check(self):
      """
      检查 self.table 是否合法
      """
      temp = set()
      if len(temp) != 1:
            raise Exception()

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

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

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

      # 正式渲染
      if isChart:
            rendered += "-" * ((col_spaces + 1) * self.length + 1) + "\n"
      for i_i in range(self.length):
            i = self.table
            if isChart:
                rendered += "|"
            for j in i:
                rendered += str(j) + " " * (col_spaces - len(str(j)))
                if isChart:
                  rendered += "|"
            if isSquare:
                if isChart:
                  rendered += "\n" + (
                        ("|" + " " * col_spaces) * (self.length + 1) + "\n"
                  ) * (col_spaces // 3)
                elif i_i != self.length - 1:
                  rendered += "\n" * (col_spaces // 3)
            else:
                rendered += "\n"
            if isChart:
                rendered += "-" * ((col_spaces + 1) * self.length + 1) + "\n"

      return rendered


print(Table(list1).render(True, True, 4))
上面是我的,下面是你的,结果明显不一样,你把参数再改一下,它的结果还是不会变:
1   123   
            
4   5   6   
            
7   8   9   
----------------------
|1   |12    |3   |
|      |      |      |      
|      |      |      |      
----------------------
|4   |5   |6   |
|      |      |      |      
|      |      |      |      
----------------------
|7   |8   |9   |
|      |      |      |      
|      |      |      |      
----------------------

还是说你还没合起来这些功能,这是你写的转换为正方形带表格的格式打印出来??{:10_277:}
但是,不得不说,做的挺好的!{:10_281:}

ckblt 发表于 2022-2-1 12:01:59

print(Table(list1).render(False, True, 4))

ckblt 发表于 2022-2-1 12:02:34

python爱好者. 发表于 2022-2-1 11:59
这是将 4 个功能整合在一起的代码吗?
为什么我运行时修改参数它的结果却不变呢:



print(Table(list1).render(False, True, 4))

python爱好者. 发表于 2022-2-1 12:06:53

ckblt 发表于 2022-2-1 12:02


原来如此{:10_256:},是我的食用方法错了{:10_266:},抱歉!

python爱好者. 发表于 2022-2-1 12:09:47

但是这样条件下打出来的似乎不像正方形啊:
list1 = [,,,,]

print(Table(list1).render(1, 1, 4))
结果是:
-------------------------------------------
|1            |897977912    |3            |
|             |             |             |            
|             |             |             |            
|             |             |             |            
|             |             |             |            
-------------------------------------------
|4            |5            |6            |
|             |             |             |            
|             |             |             |            
|             |             |             |            
|             |             |             |            
-------------------------------------------
|7            |8            |9            |
|             |             |             |            
|             |             |             |            
|             |             |             |            
|             |             |             |            
-------------------------------------------
|213213       |213213       |213          |
|             |             |             |            
|             |             |             |            
|             |             |             |            
|             |             |             |            
-------------------------------------------
|14         |214          |124          |
|             |             |             |            
|             |             |             |            
|             |             |             |            
|             |             |             |            
-------------------------------------------

python爱好者. 发表于 2022-2-1 12:10:18

长是不是有点太长了?

python爱好者. 发表于 2022-2-1 12:10:48

ckblt 发表于 2022-2-1 12:02


长是不是有点太长了?!

ckblt 发表于 2022-2-1 12:13:03

python爱好者. 发表于 2022-2-1 12:09
但是这样条件下打出来的似乎不像正方形啊:

结果是:

可能是字体不一样

python爱好者. 发表于 2022-2-1 12:13:41

那我的为什么可以?

python爱好者. 发表于 2022-2-1 12:16:17

Python 的两行的宽度的视觉效果是只等于一列的宽度,所以我在我的代码里用了 "\\ 2",
但是你算一下它的长和宽相等吗?所以应该和这个无关吧!
页: 1 2 [3] 4
查看完整版本: 【Python】命令行表格渲染器