奇怪的python行为与字典列表

时间:2012-06-12 10:39:24

标签: python

我有一个词典列表listEntries。当我在IPython中说print listEntries时,我得到以下输出。

[{'div': ' ',
  'id': '   EE 760 ',
  'instructor': 'Narayanan H.',
  'instructor_type': 'I ',
  'name': 'Advanced Network Analysis',
  'slot': '3 ',
  'student': '28'},
 {'div': ' ',
  'id': '   EE 764 ',
  'instructor': 'Karandikar Abhay',
  'instructor_type': 'I ',
  'name': 'Wireless & Mobile Communication',
  'slot': '1 ',
  'student': '36'}]

但当a = listEntries[0] print a跟随{}时,我得到的是import os import csv file1 = open('./data.txt', 'r'); csv1 = csv.reader(file1); listEntry = list(); for row in csv1 : # if first row is a number then this row represent course. course = {} if row[0].isdigit() : course['id'] = row[1] course['name'] = row[2] course['instructor'] = row[3] course['instructor_type'] = row[4] course['slot'] = row[5] course['div'] = row[6] course['student'] = row[7] else : pass listEntry.append(course); print listEntry[0]

更新:当我使用pop方法时,我得到了预期的行为。我通过解析csv文件创建了这个列表。

这是代码。

"Running courses for year 2005-2006 and semester 2 "
"   1991-1992  1992-1993  1993-1994  1994-1995  1995-1996  1996-1997  1997-1998  1998-1999  1999-2000  2000-2001  2001-2002  2002-2003  2003-2004  2004-2005  2005-2006  2006-2007  2007-2008  2008-2009  2009-2010  2010-2011  2011-2012  2012-2013    1 - Autumn 2 - Spring 3 - Summer 4 - winter     "
"Instructor Status A=Associate"
"Sr no.","Course Code","Course Name","Instructor Name","Instructor Status","Slot","Division","Enrolled students","Biometric Attendance Enabled?","Registration Limit","Restrictions","Division"
"67","   EE 760 ","Advanced Network Analysis","Narayanan H.","I ","3 "," ","28","-","0","",""
"68","   EE 764 ","Wireless & Mobile Communication","Karandikar Abhay","I ","1 "," ","36","-","0","",""

这是我正在解析的文件data.txt。我正在使用python2.7

{{1}}

2 个答案:

答案 0 :(得分:3)

发布的内容非常合适:

In [1]: L = [{'div': ' ',
  'id': '   EE 760 ',
  'instructor': 'Narayanan H.',
  'instructor_type': 'I ',
  'name': 'Advanced Network Analysis',
  'slot': '3 ',
  'student': '28'},
< {'div': ' ',
  'id': '   EE 764 ',
   ...:   'id': '   EE 760 ',
   ...:   'instructor': 'Narayanan H.',
   ...:   'instructor_type': 'I ',
   ...:   'name': 'Advanced Network Analysis',
   ...:   'slot': '3 ',
   ...:   'student': '28'},
   ...:  {'div': ' ',
   ...:   'id': '   EE 764 ',
   ...:   'instructor': 'Karandikar Abhay',
   ...:   'instructor_type': 'I ',
   ...:   'name': 'Wireless & Mobile Communication',
   ...:   'slot': '1 ',
   ...:   'student': '36'}]

In [5]: print L[0]
{'slot': '3 ', 'name': 'Advanced Network Analysis', 'instructor_type': 'I ', 'student': '28', 'div': ' ', 'instructor': 'Narayanan H.', 'id': '   EE 760 '}

In [6]: print L[1]
{'slot': '1 ', 'name': 'Wireless & Mobile Communication', 'instructor_type': 'I ', 'student': '36', 'div': ' ', 'instructor': 'Karandikar Abhay', 'id': '   EE 764 '}

In [7]: a = L[0]

In [8]: print a
{'slot': '3 ', 'name': 'Advanced Network Analysis', 'instructor_type': 'I ', 'student': '28', 'div': ' ', 'instructor': 'Narayanan H.', 'id': '   EE 760 '}

答案 1 :(得分:1)

这是一个愚蠢的错误。显然,txt文件中的前几行创建了空字典并将它们推送到listEntries。

for i in listEntries :
   if i :
       print i

修复了问题。