0

我有相当大的 csv 文件,我正在使用 csv 模块来读取 csv 文件并对其进行处理,并在我的项目中的代码片段下方进行处理。该文件在文件中有大约 9828075 条记录,代码运行良好,直到后来的第 637922 条记录,它引发以下错误:

ERROR      Tue, 14 Apr 2013 09:59:29 Traceback (most recent call last):
    File "build\bdist.win32\egg\my_proj\csv_reader.py", line 316, in next
  File "E:\MyProject\DataExa\Python26\lib\csv.py", line 104, in next
    row = self.reader.next()
  File "build\bdist.win32\egg\my_proj\csv_reader.py", line 366, in capture_record_data
IOError: [Errno 13] Permission denied

我的代码如下所示...

import csv

class MyCsvReader (object):
"""
"""

  def __init__ (self, import_source, dialect='excel'):

    """
    """
    self.import_source = import_source
    self.dialect = dialect
    self.post_init()

  def post_init(self):
    """
    Do any post init logic....
    """
    pass

  def init_reader (self):
    self.import_file = file(self.import_source, 'rU')
    #_reader = csv.reader (self.capture_record_data(self.import_file),
    #                          dialect=self.dialect)
    # create a CSV iterator that returns a dict with each next() call        
    self.reader = csv.DictReader(self.capture_record_data(self.import_file),
                                 dialect=self.dialect)
  def next (self):
    """
    Returns a dict containing data read from the CSV file.

    """
    #todo: do a magic to remove all spaces in data....
    return self.reader.next()        


  def __iter__ (self):
    "Special method to make an instance of this class into an iterator"
    return self

  def capture_record_data (self, row_iterator):
    """
    Generator for capturing the record data before passing to csv
    """
    for row in row_iterator: 
        self.raw_data = row
        yield row

  def close(self):
    if hasattr(self, 'import_file'):
        self.import_file.close()

if __name__ == '__main__':
  reader_obj =  MyCsvReader (import_source='test.csv')
  reader_obj.init_reader()
  while True:
    try:
        print reader_obj.reader.next()
    except StopIteration, e:
        break

任何人都可以帮助弄清楚为什么我会收到 IOError: [Errno 13] Permission denied 错误,同时处理文件。

4

0 回答 0