0

I have searched for a solution to this, but I have not been able to find one, weirdly.

I am opening a file with the following contents in it.

Alex,10,0,6,3,7,4
Bob, 6,3,7,2,1,8

I want to convert all the values in score_list from 1-4 index value to an integer. I have tried to do so with this following but it just doesn't work.

    score_list = []
    def opening_file():
         counter = 0
         with open('scores.txt', newline='') as infile:
             reader = csv.reader(infile)
             for row in reader:
                score_list.append(row[0:5])

             counter = 0
             while counter != 5:
               counter +=1
               row[counter] = int(row[counter])
             print (score_list)

    opening_file()

but it doesn't work and just produces

[['Alex', '10', '0', '6', '3'], ['Bob', ' 6', '3', '7', '2']]

instead of [['Alex', 10, 0, 6, 3], ['Bob', 6, 3, 7, 2]]

3 Answers 3

2

You are converting the items within row which is just a throwaway variable. Also you don't need to that redundant works, you can simply unpack your row to name and scores parts and use a list comprehension in order to convert the digits to integer.

with open('scores.txt', newline='') as infile:
    reader = csv.reader(infile)
    for row in reader:
        name, *scores = row
        score_list.append([name] + [int(i) for i in scores])
Sign up to request clarification or add additional context in comments.

5 Comments

I think you cannot concatenate arbitrary iterables to a list
@TamasHegedus Sorry I didn't got you. What you mean by arbitrary iterables?
I mean a python 3.x map object: [name]+map(int, scores)
@TamasHegedus Yeah, Indeed. Thanks for reminding that I just changed it.
I like the use of the extended unpacking here, +1
1

Your while loop transforming the values in row happens too late. Each row's values have already been copied (by a slice operation) into a new list which has been appended to score_list. And you only run the loop on the last row anyway (assuming your indentation in the question is correct).

Try something like this:

with open('scores.txt', newline='') as infile:
    reader = csv.reader(infile)
    for row in reader:
        for i in range(1,5):
            row[i] = int(row[i])
        score_list.append(row[0:5])

I'm using a for loop on a range, rather than a while loop, just because it's more convenient (a while loop version could work just fine, it just requires more lines). The key thing is to change row inside the loop on reader and before we slice the row to append to score_list.

1 Comment

Thanks a lot for that. I found all the answers helpful but I found this one in particular because you did remember to add in the code that only appends row[0:5].
1

First of all, the code converts items in the row array, but you print the score_list array. Second, as it alters the row variable outside the reader for loop, it only alters the last row. You could do something like this:

import csv

def opening_file():
  with open('scores.txt', newline='') as infile:
    return [[row[0]] + [int(x) for x in row[1:]] for row in csv.reader(infile)]
score_list = opening_file()
print(str(score_list))

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.