我们在对Population
列进行排序时遇到问题。问题是 PrettyTable 将 CSV 文件中的所有数据作为字符串读取,并将整数值排序为字符串。如何解决这个问题?
#!/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