3

With this code:

from prettytable import PrettyTable
x= PrettyTable()
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
x.add_row(["Adelaide", 1295, 1158259, 600.5])
print(x)

Prints the next:

+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
|  Adelaide | 1295 |  1158259   |      600.5      |
+-----------+------+------------+-----------------+

And what I want is to add one more column so it looks like this(I did it in excel just for illustration): enter image description here

I tried to add a row with only that value:

from prettytable import PrettyTable
x= PrettyTable()
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["City No. 1"])
print(x)

But I get an error:

edu@DESKTOP-0VFHRDP MINGW64 ~/Documents/tables (master)
$ C:/Users/edu/AppData/Local/Programs/Python/Python38-32/python.exe c:/Users/edu/Documents/tables/test.py
Traceback (most recent call last):
  File "c:/Users/edu/Documents/tables/test.py", line 5, in <module>
    x.add_row(["City No. 1"])
  File "C:\Users\edu\AppData\Local\Programs\Python\Python38-32\lib\site-packages\prettytable\prettytable.py", line 1037, in add_row
    raise Exception(
Exception: Row has incorrect number of values, (actual) 1!=4 (expected)

If you have other idea to make it I am open for recomendations. Thanks by your help.

Eduardo Herrera
  • 201
  • 1
  • 7
  • Can't mark as duplicate but this is basically it: [Python PrettyTable with multiple header/ title](https://stackoverflow.com/q/51409043/6045800) – Tomerikoo Jul 28 '21 at 23:36
  • As far as I can see, answer of the link is not solved at all, so my question is not duplicated – Eduardo Herrera Jul 28 '21 at 23:53
  • 1
    That is not the criteria for a duplicate. You are both asking the same thing so yes - this question is a duplicate. But as I said (and you noticed) it can't be closed as such because there is no upvoted or accepted answer – Tomerikoo Jul 28 '21 at 23:55
  • 2
    I get what you mean – Eduardo Herrera Jul 28 '21 at 23:58

0 Answers0