'PrettyTable incorrectly printing data from text file
The code below gives the PrettyTable output where the formatting of the data in the text file is also printed. I don't know what's the reason. Can someone help? The data in the file is formatted as shown below and is exactly as is displayed on PrettyTable's web page.
from prettytable import PrettyTable
x = PrettyTable()
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
my_file = 'pretty_print_demo.txt'
with open(my_file) as f:
for line in f:
row = line.split()
x.add_row(row)
print(x)
# FILE DATA
"City name", "Area", "Population", "Annual Rainfall" ["Adelaide", 1295,1158259, 600.5],
["Brisbane", 5905,1857594, 1146.4], ["Darwin", 11200000,120900, 1714.7],
["Hobart", 1357,205556, 619.5],
["Sydney", 2058,4336374, 1214.8],
["Melbourne", 1566,3806092, 646.9],
["Perth", 5386,1554769, 869.4]
+---------------+-------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+---------------+-------+------------+-----------------+
| ["Adelaide", | 1295, | 1158259, | 600.5], |
| ["Brisbane", | 5905, | 1857594, | 1146.4], |
| ["Darwin", | 112, | 120900, | 1714.7], |
| ["Hobart", | 1357, | 205556, | 619.5], |
| ["Sydney", | 2058, | 4336374, | 1214.8], |
| ["Melbourne", | 1566, | 3806092, | 646.9], |
| ["Perth", | 5386, | 1554769, | 869.4] |
+---------------+-------+------------+-----------------+
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
