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

我有一些SPSS输出,我已经导出为一个.txt文件(也作为各种Excel格式)。 下面是该文件的一个片段。 我需要观察和预期的相关百分比,这将发生在数百个这些输出(许多不同的对数线性模型)。 目前它被格式化为空格作为文本,而Excel作为整个行在单个单元格中。 我试图避免编写代码来破解这个问题,因为在输出的总体大小,空间的位置等方面,这也将是充满的。

有没有人有任何想法?

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

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中,您可以使用以空格作为分隔符的文本到列,以将单个单元格parsing为多个单元格。 完成后,您可以select将结果保存为字符分隔值,如果您更喜欢.csv格式。

对于Google来说,@pnuts给了我一个我忽略的简单答案。 Excel中的“文本到列”。

对于那些没有意识到(因为我不是)的,那么将SPSS数据输出转换为Excel的过程转换为可用(即单元格中的值)格式的过程如下所示:右键单击并select从SPSS导出数据输出出口,select了一个Excel格式和目的地。 或者,你可以使用这个语法(填入文件path):

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

一旦你有你的文件,你会注意到所有的数据/输出在第1列。

  1. select数据列
  2. 单击function区中的数据>文本到列
  3. select“分隔”>下一步
  4. 确保“text”和“将连续分隔符视为一个”都是checked> next
  5. 最后你可以调整数据types或简单地完成。

我知道这是一个简单的过程,但是我多年来一直在关注人们对这些事情有不同程度的了解,而经常访问Stackoverflow的人也许会这样,可能不是那个古怪的Google员工。

编辑*我正在使用的替代解决scheme(一次只有less数输出)是右键单击SPSS中的“复制特殊”,作为文本,然后在Excel中粘贴特殊,并保持源格式。 这可能会被比我聪明的人变成一个macros!

-Alex

你有没有考虑使用GENLOG而不是旧的LOGLINEAR过程,它只会产生纯文本输出? GENLOG(Analyze> Loglinear> General)生​​成正常的数据透视表输出以及残差和预测值variables,因此可以使用Output Export或OMS将输出直接导出到Excel

我必须同意JKP,使用更新的GENLOG程序可能是一个更好的方法。 但是,在某些情况下,您可能需要将文本输出转换为csv文件。 所以我喜欢分享这个解决scheme。

在这个解决scheme中,我使用SpssClient Python类来帮助查看器输出,然后利用一些本地python函数来处理文本输出并将其保存为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.