Reputation: 117
I'm using Python for a project and have data in a .txt file that is set out like this for example
Brazil.800000
United Kingdom.1200000
Zimbabwe.5000
The first 'column' is the country and the second 'column is numerical data. They're separated by a period. My task is to sort the data numerically. I've managed to make some progress using the code below from a tutorial video I found.
import csv
import operator
inputFileName = ("worldpop(2).txt")
infile = open(inputFileName, "r")
csv1 = csv.reader(infile, delimiter = ".")
sort = sorted(csv1, key = operator.itemgetter(1))
for eachline in sort:
print(eachline)
However, while this sorts the data by the second column, it considers the numerical data as strings rather than integers. Which means the result turns out as:
United Kingdom.1200000
Zimbabwe.5000
Brazil.800000
I'm totally stuck on how to get this program to see the second column as actual integers. Can you guys help?
Thanks a lot.
Edit: To clarify, I'm not trying to change the actual file, but rather just print the data in a sorted fashion to IDLE.
Upvotes: 2
Views: 4340
Reputation: 107287
You need to convert the number to int
so you should change the key to :
key=lambda x:int(x[1])
Also as a more pythonic way use with
* to open the files instead opening the file without closing! sorted
function with a proper key that sort the rows based on int
numbers :
>>> import csv
>>> from operator import itemgetter
>>> with open('infile.txt', 'r') as csvfile:
... spamreader = csv.reader(csvfile, delimiter='.')
... for row in sorted((i for i in spamreader),key=lambda x:int(x[1])):
print row
result:
['Zimbabwe', '5000']
['Brazil', '800000']
['United Kingdom', '1200000']
Also you can use reverse=True
arg in sorted
function to get a descending result :
['United Kingdom', '1200000']
['Brazil', '800000']
['Zimbabwe', '5000']
*the with
statement will close the files after that process terminated!
Upvotes: 0
Reputation: 8215
Replace the key function in your sort statement with one which converts the values to integers
sort = sorted(csv1, key = lambda x: int(x[1]))
Upvotes: 4