python-3.x 使用for循环将CSV列追加到列表

xqk2d5yq  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(179)

由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

预期输出:

['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']

实际输出:

'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 strip()和split(','),但我不能用ctv这样做,我正在将一列的数据添加到列表中,如果可以的话,请尝试用dict和list显示它,这将是很好的,但list现在是好的。

atmip9wb

atmip9wb1#

你在循环里写了print语句。这就是为什么它像这样打印输出。你需要在循环里写print输出,像这样。

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

相关问题