我有一个SPSS Python脚本,它遍历表并读取特定列的每一行值,并相应地设置参数值。目前,我正在使用getValueAt(rowIndex,colIndex)访问这些值。但是,由于表列可能会发生变化,因此必须引用列索引(而不是列名称)并不理想。有没有办法根据列名引用值?
示例代码
diagram = modeler.script.diagram()
for i in range(nrows):
jobid = job_rowset.getValueAt(i, 0);
city = job_rowset.getValueAt(i, 3);
country = job_rowset.getValueAt(i, 4);
diagram.setParameterValue ('BU', bu)
diagram.setParameterValue ('City_Param', city)
diagram.setParameterValue ('CountryID_Param', country)
任何帮助表示赞赏!谢谢!
答案 0 :(得分:2)
假设第一行将包含名称(或者您有可用的内容指定列顺序),您可以构建一个字典,其中列名称为键和列号值。
这会产生类似的结果:
name_key_dict = dict()
for i in range(ncols):
name_key_dict[colnames[i]] = i # Assuming that names is the ordered list of columns
# I would add a check here that you have the required columns
param_col_list = [ # This constructs a list of parameters vs column numbers
('BU', name_key_dict.get('Bu_Col')),
('City_Param', name_key_dict.get('City')),
('CountryID_Param', name_key_dict.get('Country Code')),
]
for row in job_rowset: # Assuming job_rowset is an iterable with a member of getValue
for (param, col) in param_col_list:
diagram.setParameterValue(param, row.getValue(col))