Search code examples
pythonsortingprettytable

How to sort Python's PrettyTable integer column correctly


We have a problem sorting a Population column. The issue is that PrettyTable reads all data from a CSV file as strings and sorts the integer values as strings. How to fix this?

#!/usr/bin/python3

from prettytable import from_csv

with open("data.csv", "r") as fp: 

    x = from_csv(fp)

x.sortby = "Population"
print(x)

data.csv

"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

Solution

  • You can do it with sort_key argument where custom key function is passed to:

    #!/usr/bin/python3
    
    from prettytable import from_csv
    
    with open("data.csv", "r") as fp: 
        x = from_csv(fp)
    
    print(x.get_string(sortby='Population', sort_key=lambda row: int(row[0])))
    

    Output:

    +-----------+----------+------------+-----------------+
    | City name |   Area   | Population | Annual Rainfall |
    +-----------+----------+------------+-----------------+
    |   Darwin  | 11200000 |   120900   |      1714.7     |
    |   Hobart  |   1357   |   205556   |      619.5      |
    |  Adelaide |   1295   |  1158259   |      600.5      |
    |   Perth   |   5386   |  1554769   |      869.4      |
    |  Brisbane |   5905   |  1857594   |      1146.4     |
    | Melbourne |   1566   |  3806092   |      646.9      |
    |   Sydney  |   2058   |  4336374   |      1214.8     |
    +-----------+----------+------------+-----------------+