简体   繁体   English

如何使用 Python 修改 SPSS 输出文件?

[英]How to modify SPSS output files with Python?

I'm making custom tables in SPSS, but when the cell values (percentages) are rounded to 1 decimal, they sometimes add up to 99,9 or 100,1 in stead of 100,0.我在 SPSS 中制作自定义表格,但是当单元格值(百分比)四舍五入到 1 位小数时,它们有时会加起来为 99,9 或 100,1 而不是 100,0。 My boss asked my to have everything neatly add up to 100. This means slightly changing some values in the output tables.我的老板让我把所有东西整齐地加起来为 100。这意味着稍微改变输出表中的一些值。

I wrote some code to retrieve cell values from tables, which works fine, but I cannot find any method or class that allows me to change cells in already generated output.我编写了一些代码来从表中检索单元格值,这工作正常,但我找不到任何允许我更改已生成输出中的单元格的方法或类。 I've tried things like :我试过这样的事情:

Table[(rij,6)] = CellText.Number(11) Table[(rij,6)] = CellText.Number(11)

and

SpssDataCells[(rij,6)] = CellText.Number(11) SpssDataCells[(rij,6)] = CellText.Number(11)

but it keeps giving me "AttributeError: 'SpssClient.SpssTextItem' object has no attribute 'DataCellArray'"但它一直给我“AttributeError:'SpssClient.SpssTextItem'对象没有属性'DataCellArray'”

How do I succesfully change cell values of output tables in SPSS?如何成功更改 SPSS 中输出表的单元格值?

My code so far:到目前为止我的代码:

import SpssClient, spss

# Python verbinden met SPSS.
SpssClient.StartClient()
OutputDoc = SpssClient.GetDesignatedOutputDoc()
OutputItemList = OutputDoc.GetOutputItems()

# Laatste tabel pakken.
lastTab = OutputItemList.Size()-2
OutputItem = OutputItemList.GetItemAt(lastTab)
Table = OutputItem.GetSpecificType()
SpssDataCells = Table.DataCellArray()

# For loop. Voor iedere rij testen of de afgeronde waarden optellen tot 100.
# Specifieke getallen pakken.
rij=0
try:
while (rij<20):
   b14 = float(SpssDataCells.GetUnformattedValueAt(rij,0))
   z14 = float(SpssDataCells.GetUnformattedValueAt(rij,1))
   zz14 = float(SpssDataCells.GetUnformattedValueAt(rij,2))
   b15 = float(SpssDataCells.GetUnformattedValueAt(rij,4))
   z15 = float(SpssDataCells.GetUnformattedValueAt(rij,5))
   zz15 = float(SpssDataCells.GetUnformattedValueAt(rij,6))
   print [b14,z14,zz14,b15,z15,zz15]
   rij=rij+1
except:
  print 'Einde tabel'

The SetValueAt method is what you require to change the value of a cell in a table. SetValueAt方法是更改​​表格中单元格值所需的方法。

PS.附注。 I think your boss should focus on more important things than to spend billable time on having percentages add up neatly to 100% (due to rounding).我认为你的老板应该专注于更重要的事情,而不是把计费时间花在让百分比加起来整齐地达到 100%(由于四舍五入)上。 Also ensure you are using as many decimal point precision as possible in your calculations so to minimize this "discrepancy".还要确保在计算中使用尽可能多的小数点精度,以尽量减少这种“差异”。

Update:更新:

Just to give an example of what you can do with manipulation like this (beyond fixing round errors):举个例子说明你可以用这样的操作做些什么(除了修复回合错误):

在此处输入图片说明

The table above shows the Share of Voice (SoV) of a Respiratory drug brand (R3) and it's rank among all brands (first two columns of data) and SoV & Rank also within it's same class of brands only (third and forth column).上表显示了呼吸药物品牌 (R3) 的占有率 (SoV) 及其在所有品牌中的排名(数据的前两列)以及仅在同一品牌类别中的 SoV 和排名(第三和第四列) . This is compared against previous month (July 15) and if the rank has increased then it is highlighted in green and an upward facing arrow is added and if declined in rank then highlighted in red and downward red facing arrow added.与上个月(7 月 15 日)进行比较,如果排名增加,则以绿色突出显示并添加一个向上的箭头,如果排名下降,则以红色突出显示并添加向下的红色箭头。 Just adds a little, color and visualization to what otherwise can be dull tables.只需为原本枯燥的表格添加一点颜色和可视化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM