Python 制表 - 如何打印输入值的垂直列表
Python Tabulate - How to print vertically list with values from an input
我正在Python中制作一个控制台程序,用户可以在其中将多个值添加到多个列表并创建通讯录。我已经将所有数据放在不同的列表中,现在我需要使用 TABULATE 模块在 table 中打印这些数据。
我可以打印这些数据,但我不想向用户显示他们的通讯录,因为在打印时,table 向我显示了垂直单行中的 NAMES 字段以及所有名称以逗号分隔,而不是应有的顺序。
我该如何解决这个问题? (在主列表中,是names和lastname的合并,这是我要打印的一些数据。
from tabulate import tabulate
start = True
names = []
lastnames = []
while(start):
print("***************************************")
print("1: Add the contact name: ")
print("2: Add the lastname for the contact: ")
print("")
op = input("Which option you want to do?: ")
if(op == "1"):
print("\n ADDING")
name = input("Please, type the name of the contact: ")
names.append(name)
lastname = input("Now, please type the lastname: ")
lastnames.append(lastname)
print("")
if(op == "2"):
print("EXIT")
start = False
nameStr = "".join(names) # Get the list in a str
lastStr = "".join(lastnames) # Get the list in a str
mainList = [[nameStr, lastStr]] # Main list, with the str data
heads = ["NAMES: ", "LASTNAMES: "] # Headers to the tabulate module
print(" ")
print(tabulate(mainList, headers=heads, tablefmt='fancy_grid', stralign='center'))
然后我得到这个列表:
您应该将每个人的名字和姓氏都打包到一个列表中,然后将该列表附加到姓名列表中:
from tabulate import tabulate
start = True
names = []
while(start):
print("***************************************")
print("1: Add the contact name: ")
print("2: Exit ")
print("")
op = input("Which option you want to do?: ")
if(op == "1"):
print("\n ADDING")
name = input("Please, type the name of the contact: ")
lastname = input("Now, please type the lastname: ")
names.append([name, lastname])
print("")
if(op == "2"):
print("EXIT")
start = False
heads = ["NAMES: ", "LASTNAMES: "] # Headers to the tabulate module
print(" ")
print(tabulate(names, headers=heads, tablefmt='fancy_grid', stralign='center'))
╒═══════════╤═══════════════╕
│ NAMES: │ LASTNAMES: │
╞═══════════╪═══════════════╡
│ Louis │ Smith │
├───────────┼───────────────┤
│ Gerard │ Taylor │
╘═══════════╧═══════════════╛
我正在Python中制作一个控制台程序,用户可以在其中将多个值添加到多个列表并创建通讯录。我已经将所有数据放在不同的列表中,现在我需要使用 TABULATE 模块在 table 中打印这些数据。
我可以打印这些数据,但我不想向用户显示他们的通讯录,因为在打印时,table 向我显示了垂直单行中的 NAMES 字段以及所有名称以逗号分隔,而不是应有的顺序。
我该如何解决这个问题? (在主列表中,是names和lastname的合并,这是我要打印的一些数据。
from tabulate import tabulate
start = True
names = []
lastnames = []
while(start):
print("***************************************")
print("1: Add the contact name: ")
print("2: Add the lastname for the contact: ")
print("")
op = input("Which option you want to do?: ")
if(op == "1"):
print("\n ADDING")
name = input("Please, type the name of the contact: ")
names.append(name)
lastname = input("Now, please type the lastname: ")
lastnames.append(lastname)
print("")
if(op == "2"):
print("EXIT")
start = False
nameStr = "".join(names) # Get the list in a str
lastStr = "".join(lastnames) # Get the list in a str
mainList = [[nameStr, lastStr]] # Main list, with the str data
heads = ["NAMES: ", "LASTNAMES: "] # Headers to the tabulate module
print(" ")
print(tabulate(mainList, headers=heads, tablefmt='fancy_grid', stralign='center'))
然后我得到这个列表:
您应该将每个人的名字和姓氏都打包到一个列表中,然后将该列表附加到姓名列表中:
from tabulate import tabulate
start = True
names = []
while(start):
print("***************************************")
print("1: Add the contact name: ")
print("2: Exit ")
print("")
op = input("Which option you want to do?: ")
if(op == "1"):
print("\n ADDING")
name = input("Please, type the name of the contact: ")
lastname = input("Now, please type the lastname: ")
names.append([name, lastname])
print("")
if(op == "2"):
print("EXIT")
start = False
heads = ["NAMES: ", "LASTNAMES: "] # Headers to the tabulate module
print(" ")
print(tabulate(names, headers=heads, tablefmt='fancy_grid', stralign='center'))
╒═══════════╤═══════════════╕
│ NAMES: │ LASTNAMES: │
╞═══════════╪═══════════════╡
│ Louis │ Smith │
├───────────┼───────────────┤
│ Gerard │ Taylor │
╘═══════════╧═══════════════╛