Dictreader如何顺序读取?

import csv

with open('grades.csv') as csvfile:
    grades_data = list(csv.DictReader(csvfile))
    
print('记录个数:', len(grades_data))
print('前2条记录:', grades_data[:2])
print('列名:', list(grades_data[0].keys()))
资料中的代码,跑结果的时候没有进行Orderdic排序
请问是什么情况?
下面是执行结果
记录个数: 2315
前2条记录: [{'assignment3_grade': '67.16444141249367', 'assignment4_grade': '53.01155312999494', 'assignment3_submission': '2015-11-12 08:58:33.998000000', 'assignment6_submission': '2015-11-22 18:31:15.934000000', 'assignment5_submission': '2015-11-20 13:24:59.692000000', 'student_id': 'B73F2C11-70F0-E37D-8B10-1D20AFED50B1', 'assignment6_grade': '38.16831825359636', 'assignment2_submission': '2015-11-09 02:22:58.938000000', 'assignment1_submission': '2015-11-02 06:55:34.282000000', 'assignment1_grade': '92.73394640624123', 'assignment5_grade': '47.710397816995446', 'assignment2_grade': '83.03055176561709', 'assignment4_submission': '2015-11-16 01:21:24.663000000'}, {'assignment3_grade': '69.7726566863439', 'assignment4_grade': '55.0981253490751', 'assignment3_submission': '2015-12-10 08:54:55.904000000', 'assignment6_submission': '2015-12-21 17:07:24.275000000', 'assignment5_submission': '2015-12-19 23:26:39.285000000', 'student_id': '98A0FAE0-A19A-13D2-4BB5-CFBFD94031D1', 'assignment6_grade': '44.62948153275085', 'assignment2_submission': '2015-12-06 17:41:18.449000000', 'assignment1_submission': '2015-11-29 14:57:44.429000000', 'assignment1_grade': '86.79082085792986', 'assignment5_grade': '49.5883128141676', 'assignment2_grade': '86.29082085792986', 'assignment4_submission': '2015-12-13 17:32:30.941000000'}]
列名: ['assignment3_grade', 'assignment4_grade', 'assignment3_submission', 'assignment6_submission', 'assignment5_submission', 'student_id', 'assignment6_grade', 'assignment2_submission', 'assignment1_submission', 'assignment1_grade', 'assignment5_grade', 'assignment2_grade', 'assignment4_submission']
 

Robin_TY - PhD@CSU,研究方向:计算机视觉,模式识别

赞同来自: fish

python中的dict结果是没有顺序的。但是DictReader返回的是一种特殊的字典类型,包含有fieldnames属性,这个属性包含的内容是构建字典时key的顺序,所以可以通过csv.DictReader(csvfile).fieldnames获取有序的列名,然后进行相应的操作。   但这种方式在实际项目中用的较少,通常用pandas.read_csv()会更为简单。

idvcn

赞同来自:

import sys f = open('BeijingPM20100101_20151231.csv','r') j = 0 for i in csv.DictReader(f):     if j <= 5:         j += 1     else:         break     print(i)     print(j) 打印结果是
OrderedDict([('8', '9'), ('2010', '2010'), ('1', '1'), ('7', '8'), ('4', '4'), ('NA', 'NA'), ('-19', '-19'), ('44', '44'), ('1017', '1017'), ('-9', '-9'), ('NW', 'NW'), ('21.02', '24.15'), ('0', '0')])
1
OrderedDict([('8', '10'), ('2010', '2010'), ('1', '1'), ('7', '9'), ('4', '4'), ('NA', 'NA'), ('-19', '-20'), ('44', '37'), ('1017', '1017'), ('-9', '-8'), ('NW', 'NW'), ('21.02', '27.28'), ('0', '0')])
2
OrderedDict([('8', '11'), ('2010', '2010'), ('1', '1'), ('7', '10'), ('4', '4'), ('NA', 'NA'), ('-19', '-19'), ('44', '37'), ('1017', '1017'), ('-9', '-7'), ('NW', 'NW'), ('21.02', '31.3'), ('0', '0')])
3
OrderedDict([('8', '12'), ('2010', '2010'), ('1', '1'), ('7', '11'), ('4', '4'), ('NA', 'NA'), ('-19', '-18'), ('44', '35'), ('1017', '1017'), ('-9', '-5'), ('NW', 'NW'), ('21.02', '34.43'), ('0', '0')])
4
OrderedDict([('8', '13'), ('2010', '2010'), ('1', '1'), ('7', '12'), ('4', '4'), ('NA', 'NA'), ('-19', '-19'), ('44', '32'), ('1017', '1015'), ('-9', '-5'), ('NW', 'NW'), ('21.02', '37.56'), ('0', '0')])
5
OrderedDict([('8', '14'), ('2010', '2010'), ('1', '1'), ('7', '13'), ('4', '4'), ('NA', 'NA'), ('-19', '-18'), ('44', '30'), ('1017', '1015'), ('-9', '-3'), ('NW', 'NW'), ('21.02', '40.69'), ('0', '0')])
6
应该是labe显示错误,是因为dict是无序引起的,这是不是影响分析结果

 

idvcn

赞同来自:

for i in csv.DictReader(f).fieldnames:     if j <= 5:         j += 1     else:         break     print(i)     print(j) 打印结果
16
1
2010
2
1
3
1
4
15
5
4
6

要回复问题请先登录注册