I;m trying to access an element of a cell from pretty table. When I tried that the title of the pretty table also comes as part of the output. is there a way to ignore it?
from prettytable import PrettyTable
table = PrettyTable(["Column 1", "Column 2", "Column 3"])
table.title = 'TESTING'
table.add_row(["A", "B", "C"])
table.add_row(["F", "O", "O"])
table.add_row(["B", "A", "R"])
for row in table:
row.border = False
row.header = False
print(row.get_string(fields = ['Column 1']).strip())
output is as follows:
| TESTING |
A
| TESTING |
F
| TESTING |
B
But i want only the fields particularly the cell values and i dont want the title. can someone kindly help.
i tried searching throught the documentation of prettytable but could not find it online.
It doesn't look like get_string
provides a straight forward API to achieve that, but we can cheat by using \r
as the temporary title and use NONE
for vrules
:
from prettytable import PrettyTable, NONE
...
for row in table:
print(row.get_string(fields=['Column 1'],
border=False,
header=False,
title='\r',
vrules=NONE).strip())
Or we can use a lower-level API:
for row in table.rows:
print(row[0])
Both will output
A
F
B
To get a specific cell's value you can use start
and stop
arguments which are zero-based and are inclusive-exclusive (like Python's range
):
print(table.get_string(fields=['Column 1'],
border=False,
header=False,
title='\r',
vrules=NONE,
start=1,
end=2).strip())
will output
F
Documentation at PyPI shows the following Style options
border - A boolean option (must be True or False). Controls whether a border is drawn around the table.
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.
hrules - Controls printing of horizontal rules after rows. Allowed values: FRAME, HEADER, ALL, NONE - note that these are variables defined inside the prettytable module so make sure you import them or use prettytable.FRAME etc.
vrules - Controls printing of vertical rules between columns. Allowed values: FRAME, ALL, NONE.
So try the following -
table.border = False
table.header = False
table.vrules = 2
table.title = ''
followed by
print(table[:])
or
for row in table:
print(row)
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.