我正在从 CSV 导入并大致以以下格式获取数据
{ 'Field1' : 3000, 'Field2' : 6000, 'RandomField' : 5000 }
字段的名称是动态的。 (嗯,它们是动态的,因为可能不止 Field1 和 Field2,但我知道Field1
and Field2
永远都会在那里。
我希望能够将这本词典传递到我的班级中allMyFields
这样我就可以将上述数据作为属性访问。
class allMyFields:
# I think I need to include these to allow hinting in Komodo. I think.
self.Field1 = None
self.Field2 = None
def __init__(self,dictionary):
for k,v in dictionary.items():
self.k = v
#of course, this doesn't work. I've ended up doing this instead
#self.data[k] = v
#but it's not the way I want to access the data.
q = { 'Field1' : 3000, 'Field2' : 6000, 'RandomField' : 5000 }
instance = allMyFields(q)
# Ideally I could do this.
print q.Field1
有什么建议么?至于为什么——我希望能够利用代码提示,并将数据导入到名为的字典中data
我一直在做的事情并没有给我带来任何好处。
(由于变量名称直到运行时才被解析,我仍然不得不向 Komodo 扔一根骨头 - 我认为self.Field1 = None
应该够了。)
那么——我该如何做我想做的事呢?或者我是在咆哮一棵设计糟糕的非Python树?