2021年3月27日星期六

sort a csv file using selection sort python

I have a csv file

name, age  jack, 28  marlin, 25  Bob, 23  jay, 30  

with selection sort, I will sort it to

name, age  Bob, 23  marlin, 25  jack, 28  jay, 30  

I try with my code. This is my code

    with open("data.csv") as file:           reader=csv.reader(file)           header=next(reader)           rows=[header]+[[row[0], float(row[1])] for row in reader if row]        for row in rows:      index = range(1, len(row[1]) - 1)      for i in index:          min_value=i              for j in range(i+1, len(row)):              if row[1][j] > row[1][min_value]:                  min_value=j            if min_value !=i:              row[1][min_value],row[1][i]=row[1][i],row[1][min_value]        print(row)  

But I have a error 'TypeError: object of type 'float' has no len()' in my code. How to fix it?

https://stackoverflow.com/questions/66828286/sort-a-csv-file-using-selection-sort-python March 27, 2021 at 01:27PM

没有评论:

发表评论