0

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

[{'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所有我得到的是{}

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

这是代码。

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]

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

"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","",""
4

2 回答 2

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 '}
于 2012-06-12T10:45:27.720 回答
1

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

for i in listEntries :
   if i :
       print i

解决了这个问题。

于 2012-06-12T12:08:06.120 回答