I am making a console program in Python where the user can add multiple values to multiple lists and create a contact book. I already have all the data in a different list, now I need to print this data in a table, with the TABULATE module.
I can print this data, but it is not as if I want to show the user their contact book, since when printing, the table shows me the NAMES field in a vertically single row with all the names separated by commas, and not one below the other as it should be.
How can I fix this? (In the main list, are the merge between names and lastname, which are some of the data I wanted to print.
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'))
Then I get this list:
You should pack both the first name and the last name into a single list for each individual and then append that list to names list:
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 │
╘═══════════╧═══════════════╛