以有意义的格式(例如 csv、tab)导出 SPSS 输出?

Export SPSS output in a meaningful format (e.g. csv, tab)?

我有一些来自 SPSS 的输出,我已将其导出为 .txt 文件(但也可以导出为各种 Excel 格式)。下面是该文件的一个片段。我需要观察到的和预期的相关百分比,这将发生在数百个这样的输出(许多不同的对数线性模型)中。目前它的格式是空格作为文本,并且作为 Excel 它在一个单元格中有整行。我试图避免必须编写代码来破译它,因为考虑到输出的整体大小、其中空格的位置等,这也会令人担忧。

有没有人有什么想法?

一个明确的解决方案是使用不同的包,但是在 SPSS LOGLINEAR 语法中指定多项式对比并为我计算权重的能力无法在其他地方轻松实现(即它可能是可能的,但它超出了我的技能)。

 Observed, Expected Frequencies and Residuals

       Factor          Code              OBS. count & PCT.   EXP. count & PCT.      Residual   Std. Resid.   Adj. Resid.


  deg_code        0
   res_code        100
    edge_cod        2.520                    13.00 (  .29)      112.75 ( 2.56)      -99.7500       -9.3941       -9.8118
    edge_cod        0.630                    40.00 (  .91)      112.75 ( 2.56)      -72.7500       -6.8513       -7.1560
    edge_cod        0.315                   130.00 ( 2.95)      112.75 ( 2.56)       17.2500        1.6245        1.6968
    edge_cod        0.105                   194.00 ( 4.40)      112.75 ( 2.56)       81.2500        7.6518        7.9921
   res_code        85
    edge_cod        2.520                    21.00 (  .48)      112.75 ( 2.56)      -91.7500       -8.6407       -9.0249
    edge_cod        0.630                    60.00 ( 1.36)      112.75 ( 2.56)      -52.7500       -4.9678       -5.1887
    edge_cod        0.315                   136.00 ( 3.08)      112.75 ( 2.56)       23.2500        2.1896        2.2870
    edge_cod        0.105                   194.00 ( 4.40)      112.75 ( 2.56)       81.2500        7.6518        7.9921
   res_code        70
    edge_cod        2.520                    70.00 ( 1.59)      112.75 ( 2.56)      -42.7500       -4.0260       -4.2051
    edge_cod        0.630                   117.00 ( 2.65)      112.75 ( 2.56)        4.2500         .4002         .4180
    edge_cod        0.315                   164.00 ( 3.72)      112.75 ( 2.56)       51.2500        4.8265        5.0412
    edge_cod        0.105                   214.00 ( 4.85)      112.75 ( 2.56)      101.2500        9.5354        9.9594

  deg_code        8
   res_code        100
    edge_cod        2.520                    17.00 (  .39)      120.08 ( 2.72)     -103.0833       -9.4069       -9.8252
    edge_cod        0.630                    56.00 ( 1.27)      120.08 ( 2.72)      -64.0833       -5.8480       -6.1080
    edge_cod        0.315                   138.00 ( 3.13)      120.08 ( 2.72)       17.9167        1.6350        1.7077
    edge_cod        0.105                   195.00 ( 4.42)      120.08 ( 2.72)       74.9167        6.8366        7.1405
   res_code        85
    edge_cod        2.520                    27.00 (  .61)      120.08 ( 2.72)      -93.0833       -8.4944       -8.8721
    edge_cod        0.630                    72.00 ( 1.63)      120.08 ( 2.72)      -48.0833       -4.3879       -4.5830
    edge_cod        0.315                   146.00 ( 3.31)      120.08 ( 2.72)       25.9167        2.3650        2.4702
    edge_cod        0.105                   200.00 ( 4.54)      120.08 ( 2.72)       79.9167        7.2928        7.6171
   res_code        70
    edge_cod        2.520                    82.00 ( 1.86)      120.08 ( 2.72)      -38.0833       -3.4753       -3.6298
    edge_cod        0.630                   119.00 ( 2.70)      120.08 ( 2.72)       -1.0833        -.0989        -.1033
    edge_cod        0.315                   172.00 ( 3.90)      120.08 ( 2.72)       51.9167        4.7377        4.9483
    edge_cod        0.105                   217.00 ( 4.92)      120.08 ( 2.72)       96.9167        8.8442        9.2374

  deg_code        16
   res_code        100
    edge_cod        2.520                    39.00 (  .88)      134.67 ( 3.05)      -95.6667       -8.2439       -8.6104
    edge_cod        0.630                    67.00 ( 1.52)      134.67 ( 3.05)      -67.6667       -5.8310       -6.0903
    edge_cod        0.315                   132.00 ( 2.99)      134.67 ( 3.05)       -2.6667        -.2298        -.2400
    edge_cod        0.105                   211.00 ( 4.78)      134.67 ( 3.05)       76.3333        6.5779        6.8703
   res_code        85
    edge_cod        2.520                    48.00 ( 1.09)      134.67 ( 3.05)      -86.6667       -7.4683       -7.8004
    edge_cod        0.630                    87.00 ( 1.97)      134.67 ( 3.05)      -47.6667       -4.1076       -4.2902
    edge_cod        0.315                   161.00 ( 3.65)      134.67 ( 3.05)       26.3333        2.2692        2.3701
    edge_cod        0.105                   212.00 ( 4.81)      134.67 ( 3.05)       77.3333        6.6640        6.9603
   res_code        70
    edge_cod        2.520                    92.00 ( 2.09)      134.67 ( 3.05)      -42.6667       -3.6767       -3.8402
    edge_cod        0.630                   151.00 ( 3.42)      134.67 ( 3.05)       16.3333        1.4075        1.4701
    edge_cod        0.315                   195.00 ( 4.42)      134.67 ( 3.05)       60.3333        5.1991        5.4303
    edge_cod        0.105                   221.00 ( 5.01)      134.67 ( 3.05)       86.3333        7.4396        7.7704

在 Excel 中,您可以使用以 space 作为分隔符的文本分栏来将单个单元格解析为多个单元格。这样做之后,如果您更喜欢 .csv 格式,您可以选择将结果保存为字符分隔值。

对于来自 Google 的那些人,@pnuts 给了我一个我忽略的简单答案; "Text to Columns" 在 Excel 中。

对于那些不知道的人(我不知道),将 SPSS 数据输出 Excel 转换为可用格式(即单元格中的值)的过程如下: 通过右键单击并选择导出,从 SPSS 导出数据输出,选择 excel 格式和目标。或者,您可以使用此语法(填写文件路径):

   *Export Output to Excel.
  OUTPUT EXPORT
  /CONTENTS  EXPORT=ALL  LAYERS=PRINTSETTING  MODELVIEWS=PRINTSETTING
  /XLS  DOCUMENTFILE='C:\FILEPATHHERE'
     OPERATION=CREATEFILE
     LOCATION=LASTCOLUMN  NOTESCAPTIONS=YES.

获得文件后,您会注意到所有 data/output 都在第 1 列中。

  1. Select数据列
  2. 单击功能区中的数据 > 文本到列
  3. Select "delimited" > 下一个
  4. 确保 "text" 和 "treat consecutive delimiters as one" 都被选中 > 接下来
  5. 最后你可以调整数据类型或者干脆完成。

我知道这是一个简单的过程,但多年来我一直认为人们对这些事情的了解程度不同,虽然经常访问 Whosebug 的人可能会遇到这个问题,但奇怪的 Google r 可能不是。

编辑* 我目前使用的另一种解决方案(一次仅用于几个输出)是在 SPSS 中右键单击 'copy special' 作为文本,然后在 excel 中粘贴特殊内容并保留源格式。可能比我聪明的人把它做成宏!

-亚历克斯

您是否考虑过使用 GENLOG 而不是非常古老的 LOGLINEAR 过程,它只产生纯文本输出? GENLOG (Analyze>Loglinear>General) 产生常规主元 table 输出以及残差和预测值变量,因此它的输出可以使用输出导出或 OMS 直接导出到 Excel,无需在Excel

我不得不同意 JKP,使用较新的 GENLOG 程序可能是更好的方法。但是,在某些情况下,您可能希望将文本输出转换为 csv 文件。因此我喜欢分享这个解决方案。

在此解决方案中,我借助 SpssClient Python class grep 查看器输出,然后使用一些本机 函数来操作文本输出并将其另存为csv 文件。

作为示例数据,我使用来自 SPSS 示例文件的 'demo.sav'。

**** create some sample output ****.

GET FILE='/opt/IBM/SPSS/Statistics/22/Samples/English/demo.sav'.

LOGLINEAR inccat (1,4) jobsat (1,5)
   /DESIGN inccat.

LOGLINEAR inccat (1,4) ownpc (0,1)
   /DESIGN inccat.

**** transform output to csv files ****

BEGIN PROGRAM.
import csv
import re
import os
import SpssClient

# define directory where the csv files will be stored
os.chdir("/your/path/to/directory/")

# define csv file basename
basename = 'loglin'

# define delimeter for csv files
delim = ';'


SpssClient.StartClient()

OutputDoc = SpssClient.GetDesignatedOutputDoc()
OutputItems = OutputDoc.GetOutputItems()

# create list wich contains the several text outputs 
# of the LogLinear procedures
TextItems = []

for index in range(OutputItems.Size()):
   OutputItem = OutputItems.GetItemAt(index)
   if (OutputItem.GetType() == SpssClient.OutputItemType.TEXT 
   and OutputItem.GetProcedureName() == 'Loglinear'):
      TextItem = OutputItem.GetSpecificType()
      TextItems.append(TextItem.GetTextContents())

SpssClient.StopClient()

# some fine tuning, so that actual values get into
# one table cell, without brackets and stuff like that
def stringadj (astring):
   astring = re.sub(r' +\( *', delim, astring) # replace left bracket by delimeter
   astring = re.sub(r'\)', '', astring) # remove right bracket
   astring = re.sub(r' & ', delim, astring) # replace '&' by delimeter
   astring = re.sub(r'= ?', '=' + delim, astring) # replace '=' by delimeter
   astring = re.sub(delim + delim, delim, astring) # replace double delimeter by single delimeter
   return astring

# define cvs style
csv.register_dialect('loglinstyle', delimiter=delim)

# split output strings into several lines
# do necessary text adjustments
# and store each output in a seperate (enumerated) csv file
for position, item in enumerate(TextItems):
   lltable = [re.sub(r' ( )+', delim, line.lstrip().rstrip()) for line in item.split("\n")]

   lltable = [stringadj(line) for line in lltable]

   filename = basename + str(position+1) + '.csv'

   with open(filename, 'wb') as f:
      writer = csv.writer(f, 'loglinstyle')
      writer.writerows(csv.reader(lltable, 'loglinstyle'))

END PROGRAM.