有没有一个Python模块打开SPSS文件? [英] Is there a Python module to open SPSS files?

查看:1108
本文介绍了有没有一个Python模块打开SPSS文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python是否有一个模块来打开IBM SPSS(即.sav)文件?如果有最新的东西是不需要任何额外的dll文件/库,那将是巨大的。

解决方案

关于您要做什么 - 使用 rpy2 中的R相关命令处理数据,或切换到Python - < @Spacedman提供的一个href =http://stackoverflow.com/a/19297044/420055解决方案可能很容易适应您的需求。



否则,熊猫包含 rpy2 的便利包装。以下是与Peat和Barton的 weights.sav 数据集一起使用的示例:

 >>>将pandas.rpy.common导入为com 
>>> filename =weights.sav
>>> w = com.robj.r('foreign :: read.spss(%s,to.data.frame = TRUE)'%filename)
>>> w = com.convert_robj(w)
>>> w.head()
ID重量长度头饰教育学士学位
1 L001 3.95 55.5 37.5女大专3名以上兄弟姐妹
2 L003 4.63 57.0 38.5女性单身人士
3 L004 4.75 56.0 38.5男年12兄弟姐妹
4 L005 3.92 56.0 39.0男高级一兄弟
5 L006 4.56 55.0 39.5男年10兄弟姐妹


Is there a module for Python to open IBM SPSS (i.e. .sav) files? It would be great if there's something up-to-date which doesn't require any additional dll files/libraries.

解决方案

Depending on what you want to do--process data using R-related commands from rpy2, or switch to Python--the solution provided by @Spacedman on a related thread might easily be adapted to suit your needs.

Otherwise, Pandas includes a convenient wrapper for rpy2. Here is an example of use with Peat and Barton's weights.sav data set:

>>> import pandas.rpy.common as com
>>> filename = "weights.sav"
>>> w = com.robj.r('foreign::read.spss("%s", to.data.frame=TRUE)' % filename)
>>> w = com.convert_robj(w)
>>> w.head()
     ID  WEIGHT  LENGTH  HEADC  GENDER  EDUCATIO              PARITY
1  L001    3.95    55.5   37.5  Female  tertiary  3 or more siblings
2  L003    4.63    57.0   38.5  Female  tertiary           Singleton
3  L004    4.75    56.0   38.5    Male    year12          2 siblings
4  L005    3.92    56.0   39.0    Male  tertiary         One sibling
5  L006    4.56    55.0   39.5    Male    year10          2 siblings

这篇关于有没有一个Python模块打开SPSS文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆