我正在尝试读取CSV并将其添加到对象列表中。
这是类代码
class ReportItem:
'''
classdocs
'''
def __init__(self, hostname, jdbcDbname):
'''
Constructor
'''
self.hostname = hostname
self.jdbcDbname = jdbcDbname
self.dbname = None
self.schema = None
self.shortName = None
self.totalRows = None
self.archivedRows = None
self.deletedRows = None
self.deletedSizeMB = None
self.totalSizeMB = None
self.archivedSizeMB = None
self.deletedSizeMB = None
def setDbname(self, dbname):
self.dbname = dbname
def setSchema(self, schema):
self.schema = schema
def setShortName(self, shortName):
self.shortName = shortName
def setTotalRows(self, totalRows):
self.totalRows = totalRows
def setArchivedRows(self, archivedRows):
self.archivedRows = archivedRows
def setDeletedRows(self, deletedRows):
self.deletedRows = deletedRows
def setTotalSizeMB(self, totalSizeMB):
self.totalSizeMB = totalSizeMB
def setArchivedSizeMB(self, archivedSizeMB):
self.archivedSizeMB = archivedSizeMB
def setDeletedSizeMB(self, deletedSizeMB):
self.deletedSizeMB = deletedSizeMB
这是实施代码:
import csv
from ReportItem import ReportItem
reportList = []
with open('Tables.csv', 'r') as csvfile:
has_header = csv.Sniffer().has_header(csvfile.read(1024))
csvfile.seek(0)
csvReader = csv.reader(csvfile, delimiter=',', quotechar='\'')
if has_header:
next(csvfile, None)
for col in csvReader:
reportItem = ReportItem(col[0], col[1])
reportItem.setDbname(col[2])
reportItem.setSchema(col[3])
reportItem.setShortName(col[4])
reportItem.setTotalRows(col[5])
reportItem.setArchivedRows(col[6])
reportItem.setDeletedRows(col[7])
reportItem.totalSizeMB(col[8])
reportItem.archivedSizeMB(col[9])
reportItem.setDeletedSizeMB(col[10])
reportList.append(reportItem)
for ri in reportList:
print(ri.dbname)
CSV内容如下所示: xxx xx xxxx xxxx xxxxx -1 0.06 -1 -1 -1 -1
这导致了错误:
Traceback (most recent call last):
File "CSVImporter.py", line 25, in <module>
reportItem.totalSizeMB(col[8])
TypeError: 'NoneType' object is not callable
另一个问题是这个col [8]也可以用科学记数法表示,如1.000E5。解决这个问题的最佳方法是什么?
答案 0 :(得分:3)
我想我找到了原因。在实现中,您将调用在类的 init 函数中定义的totalSizeMB变量。变量不带参数。 (也许你的意思是函数,setTotalSizeMB)
如果是这种情况,那么它下面就存在同样的问题(archivedSizeMB)
答案 1 :(得分:2)
这看起来像一个错字。你的代码应该是
reportItem.setTotalSizeMB(col[8])
而不是
reportItem.totalSizeMB(col[8])
班上没有totalSizeMB功能。