rnsleep
rnsleep

Reputation: 3

appending a CSV column to a list using a for loop

Code written by Abdulmalik

import csv

def loadCSVData(filename):
    list = [] #list for storing file content
    with open(filename, newline='') as file:# 
        fileContent = csv.DictReader(file)
        for line in fileContent:
            list.append(line['Score'])
            print(list)
        fileContent.close()
    return list

expected output:

['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1', '89.8', '89.5', '89.2', '89', '88.8', '88.6', '88.4', '88.2', '88', '87.8', '87.6', '87.5', '87.3', '87.2', '87', '86.9', '86.8', '86.6', '86.5', '86.4', '86.3', '86.1', '86', '85.9', '85.8', '85.7', '85.6', '85.5', '85.4', '85.3', '85.2', '85.1', '85', '85', '84.9', '84.8', '84.7', '84.6', '84.5', '84.5', '84.4', '84.3', '84.2', '84.2', '84.1', '84', '84', '83.9', '83.8', '83.8', '83.7', '83.6', '83.6', '83.5', '83.4', '83.4', '83.3', '83.3', '83.2', '83.1', '83.1', '83', '83', '82.9', '82.9', '82.8', '82.8', '82.7', '82.6', '82.6', '82.5', '82.5', '82.4', '82.4', '82.3', '82.3', '82.2', '82.2', '82.2', '82.1', '82.1', '82', '82', '81.9', '81.9', '81.8', '81.8', '81.8']

actual output:


'100']
['100', '96.7']
['100', '96.7', '95.1']
['100', '96.7', '95.1', '94.1']
['100', '96.7', '95.1', '94.1', '93.3']
['100', '96.7', '95.1', '94.1', '93.3', '92.6']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1', '89.8'] 
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1', '89.8', '89.5']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1', '89.8', '89.5', '89.2']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1', '89.8', '89.5', '89.2', '89']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1', '89.8', '89.5', '89.2', '89', '88.8']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1', '89.8', '89.5', '89.2', '89', '88.8', '88.6']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1', '89.8', '89.5', '89.2', '89', '88.8', '88.6', '88.4']
['100', '96.7', '95.1', '94.1', '93.3', '92.6', '92', '91.5', '91.1', '90.7', '90.4', '90.1', '89.8', '89.5', '89.2', '89', '88.8', '88.6', '88.4', '88.2']

Too long to paste the whole outcome, that's my attempt so far, my main goal is too strip(), and split(',') but I can't do that with ctv, I am adding the data from one column to the list, if you can please try to show it with dict and list that would be wounderful, but list is good for now.

Upvotes: 0

Views: 58

Answers (1)

KapBytes Technologies
KapBytes Technologies

Reputation: 17

You writen print statement inside the loop .That's why it's printing output like this. You need to write print out side from a loop, like this.

with open(filename, newline='') as file:# 
    fileContent = csv.DictReader(file)
    for line in fileContent:
        list.append(line['Score'])
    print(list)
    fileContent.close()

Upvotes: 1

Related Questions