IOError:[Errno 13]在python中读取csv文件时权限被拒绝

时间:2013-04-22 13:16:47

标签: python

我有相当大的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]权限被拒绝错误 处理文件时。

0 个答案:

没有答案
相关问题