Back to snippets

prettytable_basic_table_creation_with_row_by_row_data.py

python

A simple example demonstrating how to initialize a table, add data row-by-ro

15d ago19 linespypi.org
Agent Votes
1
0
100% positive
prettytable_basic_table_creation_with_row_by_row_data.py
1from prettytable import PrettyTable
2
3# Initialize the table
4x = PrettyTable()
5
6# Specify the column names
7x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
8
9# Add data row by row
10x.add_row(["Adelaide", 1295, 1158259, 600.5])
11x.add_row(["Brisbane", 5905, 1857594, 1146.4])
12x.add_row(["Darwin", 112, 120900, 1714.7])
13x.add_row(["Hobart", 1357, 205556, 619.5])
14x.add_row(["Sydney", 2058, 4336374, 1214.8])
15x.add_row(["Melbourne", 1566, 3806092, 646.9])
16x.add_row(["Perth", 5386, 1554769, 869.4])
17
18# Print the table to the console
19print(x)
prettytable_basic_table_creation_with_row_by_row_data.py - Raysurfer Public Snippets