我有以下代碼(片段)...它可能對別人有用......但我有一個問題...我的一個類變量似乎不是工作,好像你可以使用的變量數量有限制?AttributeError:添加新類變量時對象沒有任何屬性
import csv
class MASAT_data:
#
# ALL CSV files are expected to have the first row as headers
# ALL CSV files are expected to have the first column as the key attribute
# All Headers are extracted into its own object variable
#
def __init__(self, fname, action, intest):
__data_Dict = {}
__fname = fname
__header = [] # <--- THIS WORKS
__colLEN = 0
__rowLEN = 0
__data_rows = [] # <--- This seems to not be available to the class
#__test = intest
if action.upper == "NEW":
pass
else:
self.Get_DICT(__fname)
def Get_DICT(self,filename):
templist = []
daDict = {}
with open(filename,"r") as f:
csv_f = csv.reader(f)
x=-1
for row in csv_f:
x+=1
if len(row)!=0:
if x == 0:
self.__header = row
self.__colLEN = len(self.__header)
else:
templist = row
self.__data_rows.append(templist) # <-- line 38
if len(templist) >= 2:
daDict[templist[0]] = templist[1:]
self.__rowLEN = x
f.close()
self.__data_Dict = daDict
def rtnData(self,keyName):
return self.__data_Dict[keyName]
def hlen(self):
return self.__colLEN
def __len__(self):
return self.__rowLEN
def rtnList(self):
templist = [[]]
temps = []
for key, value in self.__data_Dict.items():
temps.append(key)
for x in value:
temps.append(x)
templist.append(temps)
temps = []
templist.pop(0)
#for x in range(len(templist)):
# print(templist[int(x)])
return templist
def rtnListDict(self):
templist = []
for x in range(len(self.__header)):
print (self.__header[x])
#print (self.__data_Dict[self.__header[x]])
我得到以下錯誤:
C:\Python34\python.exe C:/Users/User/PycharmProjects/MASAT-3/MA-SAT_v3_MAIN.py
Traceback (most recent call last):
File "C:/Users/User/PycharmProjects/MASAT-3/MA-SAT_v3_MAIN.py", line 10, in <module>
appCONFIGs = MASAT_data("CONFIG.csv","LOAD",t)
File "C:\Users\User\PycharmProjects\MASAT-3\MASAT_Libs.py", line 23, in __init__
self.Get_DICT(__fname)
File "C:\Users\User\PycharmProjects\MASAT-3\MASAT_Libs.py", line 38, in Get_DICT
self.__data_rows.append(templist)
AttributeError: 'MASAT_data' object has no attribute '_MASAT_data__data_rows'
過程與退出代碼完成1
調用程序如下:
__author__ = 'User'
import sys
from MASAT_Libs import *
sys.path.append('C:\\Users\\User\\PycharmProjects\MASAT-3')
t = 0
#define Globals
if __name__ == '__main__':
appCONFIGs = MASAT_data("CONFIG.csv","LOAD",t)
if appCONFIGs.rtnData("ApplicationMode")[0] == 'DEBUG':
t = 1
else:
t = 0
print("t = ",t)
observations = MASAT_data(appCONFIGs.rtnData("PathName")[0] + "\\Observables.csv", "LOAD",t)
KMNs = MASAT_data(appCONFIGs.rtnData("PathName")[0] + "\\KMN_List.csv", "LOAD",t)
geomix = MASAT_data(appCONFIGs.rtnData("PathName")[0] + "\\geomix_perf_list.csv", "LOAD",t)
print (geomix.rtnList())
print (len(geomix))
geomix.rtnListDict()
所以請原諒我放在一個額外的空間,或沒有正確縮進(我在這裏處理代碼時手動完成了...
我的問題是我不能在這個類中加入另一個變量(不管名字是什麼)。任何想法一個簡單的修復?
啊,對了,忘了說了,當我刪除了2號線(在__init__函數的聲明,在線38上的一個...一切都很好...... – Oogobuk