简体   繁体   中英

Removing the column headings off my Prettytable table

My project is to code a program that mimics a simple percolation process. When i run my program my output is a table with column headings as well. How do i remove this? i only need my random numbers to appear

this is the code i tried

import random
from prettytable import PrettyTable


def get_dimensions():
    return input("Enter the dimensions Ex:(5x5): ")


def validate(dim):
    if dim == "":
        dim = "5x5"
    try:
        rows = int(dim[0])
        columns = int(dim[2])
        if not (3 <= rows <= 100) or not (3 <= columns <= 100):
            print("The inputs of the dimensions MUST BE in the range of 3-9 inclusive.\nTry again!")
            return [True, 0, 0]
    except:
        print("The input format is not valid!\nPlease read the instructions and try again!\n")
        return [True, 0, 0]
    return [False, rows, columns]


def create_grid(rows, columns):
    numbers = list(range(10, 100)) + ["  "]
    main = [[random.choice(numbers) for k in range(columns)] for i in range(rows)]
    column_names = list(range(columns))
    table = PrettyTable(column_names)
    for row in main:
        table.add_row(row)
    print(table)
    return main


def check_percolation(main, columns):
    for i in range(columns):
        for row in main:
            if row[i] == "  ":
                print("  NO", end=" ")
                break
        else:
            print("  OK", end=" ")


def main():
    while True:
        dimension = get_dimensions()
        validation = validate(dimension)
        not_validated = validation[0]
        rows = validation[1]
        columns = validation[2]
        if not_validated:
            continue

        main = create_grid(rows, columns)
        check_percolation(main, columns)

        cont = input("\n\nDo you wish to continue? (Y/N): ")
        if cont.upper() == "N":
            break


main()

expected output

output i got

To hide column headings, put table.header = False before printing table in create_grid .

Style options:

[snip]

  • header - A boolean option (must be True or False ). Controls whether the first row of the table is a header showing the names of all the fields. https://pypi.org/project/prettytable/

Alternatively, change the print to

print(table.get_string(header=False))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM