I am trying to open a csv file and convert the values from strings to integers so I can sort the list. Currently when I sort the list the results I get are "[[], ['190'], ['200'], ['250'], ['350'], ['90']]"
. Here is my code.
import csv
def bubbleSort(scores):
for length in range(len(scores)-1,0,-1):
for i in range(length):
if scores[i]>scores[i+1]:
temp = scores[i]
scores[i] = scores[i+1]
scores[i+1] = temp
with open ("rec_Scores.csv", "rb") as csvfile:
r = csv.reader(csvfile)
scores = list(r)
bubbleSort(scores)
print(scores)
This is probably really easy to fix, but I am still new to python so if anyone could help me solve this problem it would be much appreciated.
You need to add scores_int = [int(score) for score in scores] in order to convert the string numbers in your scores list to int numbers. This is what your code should look like:
import csv
def bubbleSort(scores):
for length in range(len(scores)-1,0,-1):
for i in range(length):
if scores[i]>scores[i+1]:
temp = scores[i]
scores[i] = scores[i+1]
scores[i+1] = temp
with open ("rec_Scores.csv", "rb") as csvfile:
r = csv.reader(csvfile)
scores = list(r)
scores_int = [int(score) for score in scores]
bubbleSort(scores_int)
print(scores)