0

This is my python script for sorting by column a csv file read from stdin:

with  sys.stdin as csvfile:
  reader = csv.reader(csvfile, delimiter=',')
  sortedlist = sorted(reader, key=operator.itemgetter(2))
  for row in sortedlist:
     print(','.join(row)),
     print('\n'),

I run the command in order to sort by 3rd column(zero indexed is 2):

./sorter1.py < test.csv > test_sorted.csv

and the sorted file test_sorted.csv is:

31,53,101,122 
88,95,103,59 
66,58,104,50 
93,46,105,52 
88,88,118,107 
**115,57,31,34** 
110,87,36,63 
32,108,36,107 
75,35,57,35 
99,46,57,28 
41,35,67,59 
108,99,98,35 
36,66,98,60 

It is like sorted two files and merged it. Is it a matter of buffer size of the reader or a matter of sorted method?

This sorts alphabetically not numerically

1
  • 2
    Where was column defined and what value is it? Commented Oct 9, 2016 at 18:27

1 Answer 1

1

This sorts alphabetically not numerically

You'll have to cast the sort column values to integer in order to sort numerically. I have replaced the sort function with a lambda that includes the conversion to int:

sortedlist = sorted(reader, key=lambda x: int(x[2]))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.