0

so i'm trying to sort by value bunch of data from csv file , but the output is wrong . so any opinion ? Note : i should solve it without pandas.thanks

import csv
import operator

with open('final.csv', 'r') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        sortedlist = sorted(reader, key=operator.itemgetter(1), reverse=True)

for eachline in sortedlist:
    print(eachline)

csvFile.close()

and the OUTPUT :

['sara', '9.75']
['soheila', '7.833333333333333']
['hamid', '6.066666666666666']
['ali', '5.0']
['sarvin', '11.375']
['sina', '11.285714285714286']
  • https://stackoverflow.com/q/64083565/12705907 might help – Senthil Vikram Vodapalli Nov 16 '21 at 18:31
  • The structure of your code is good. You want to sort on the second column of values as if they are floats, not text. So that's at least one part of the solution. Next, do you want the values to actually be (converted to) floats, or left as strings and merely *interpreted* as floats by the sorter? – Zach Young Nov 16 '21 at 20:01
  • This question, (https://stackoverflow.com/a/26725820/246801), is about ints, not floats (like you have), but it's the same question, and the solution I think you'd like. – Zach Young Nov 16 '21 at 20:03
  • @ZachYoung yes the final output should be string . and I realized by your answer that the sorter should deal with floats . could you explain to me ? thanks – Rana Abadi Nov 16 '21 at 20:10
  • Did you check out that link I suggested? Look at the first line of the accepted solution, `sorted(XWordDict...` and how they are using `float()` and `itemgetter()` as part of the `key` parameter. Give that a try, and if it doesn't work, update your code and we can take a look. – Zach Young Nov 16 '21 at 20:12
  • @ZachYoung it worked!!! Thank you ! it was very helpful :) – Rana Abadi Nov 16 '21 at 20:32
  • Also, I can see you were able to help yourself, but if anything below helped, (setting `sortedList` inside the for-loop is not best), please give them an upvote. – Zach Young Nov 16 '21 at 20:42

2 Answers2

1

for row in reader:
        sortedlist = sorted(reader, key=operator.itemgetter(1), reverse=True)
   

for row in reader will consume the first row.
Then you are sorting reader, this will sort all lines except the first line

Guess you can remove the for loop, and just have


        sortedlist = sorted(reader, key=operator.itemgetter(1), reverse=True)
   

Were you looking for something else ?

ecsridhar
  • 111
  • 5
1

First get all the rows in a list and then sort the list by the value:

import csv
import operator

rows = []
with open('final.csv', 'r') as csvFile:
    reader = csv.reader(csvFile)
    for row in reader:
        rows.append(row)

sorted(rows, key=lambda x: x[1])
Franco Piccolo
  • 6,845
  • 8
  • 34
  • 52
  • it gave me the same output unfortunately. – Rana Abadi Nov 16 '21 at 19:53
  • That shouldn't be so, why dont you print the exact command i sent you print(sorted(rows, key=lambda x: x[1])) – Franco Piccolo Nov 16 '21 at 19:58
  • 1
    This has the same problem as escridhar's solution, the second column is sorted as text, not floats. I asked OP for clarification as to whether the second column should be converted to float, or just interpreted as float for the sort. – Zach Young Nov 16 '21 at 20:02