好吧,讀完你的代碼後,我的第一反應是OMG!他有多少人打開那個糟糕的文件?
這是你的類的新版本
class CSV:
def __init__(self, file=None):
self.file = file
with open(self.file, 'r') as f:
self.data = [row for row in csv.reader(f)]
def get_row_count(self):
return len(self.data)
def get_column_count(self):
return len(self.data[0])
def get_data(self, rows=1):
return self.data
我也是固定的csv.reader()
處理。它接受一個文件對象,不需要.read()
或.read().splitlines()
,它只會導致錯誤。這可能是它失敗的原因。
好吧,根據您的說法,您正在使用AWS,並且您的文件不是文件的字符串路徑,而是文件對象。所以你不需要open()
部分。您可能需要修改您的代碼,以便它如下:
class CSV:
def __init__(self, f=None):
self.file = f
if isinstance(self.file, str): # if the file is a string, it's a path that has to be opened
with open(self.file, 'r') as f:
self.data = [row for row in csv.reader(f)]
elif isinstance(self.file, File) or isinstance(self.file, file): # if that's a file object, no need to open
self.data = [row for row in csv.reader(self.file)]
else: # otherwise, I don't know what to do, so aaaaaaaargh!
raise Exception("File object type unknown: %s %s" % (type(file), file,))
def get_row_count(self):
return len(self.data)
def get_column_count(self):
return len(self.data[0])
def get_data(self, rows=1):
return self.data
讀S3BotoStorage.py的S3BotoStorage類從django.core.files.base.File,從django.core.files繼承繼承。 utils.FileProxyMixin,它是全局python file
類的屬性的組合。
因此,File
對象不是file
的實例,但它具有兼容的接口。因此,在前面的代碼中,我測試了self.file
是否爲str
,那麼它應該是我們的路徑open()
,因此我們得到file()
並解析它。否則,self.file
是File
對象或file()
對象,我們只需要解析它。如果這兩者都不是,那麼這是一個錯誤,我們將除外。
來源
2013-06-26 09:37:46
zmo
你可以添加堆棧跟蹤嗎? – Wolph
'.splitlines()'返回一個列表。 csv.reader不需要列表我相信 –
沒有必要添加任何'csv.reader(f)'就夠了! – zmo