读取spss文件java

问题描述 投票:0回答:2
  SPSSReader reader = new SPSSReader(args[0], null);
            Iterator it = reader.getVariables().iterator();
            while (it.hasNext())
             {
                System.out.println(it.next());
            }

我正在使用此SPSSReader来读取spss文件。在这里,每个字符串都打印有一些附加了它的垃圾字符。

获得的结果:

StringVariable: nameogr(nulltpc{)(10)
NumericVariable: weightppuo(nullf{nd)
DateVariable: datexsgzj(nulllanck)
DateVariable: timeppzb(null|wt{l)
DateVariable: datetimegulj{(null|ns)
NumericVariable: commissionyrqh(nullohzx)
NumericVariable: priceeub{av(nullvlpl)

预期结果 :

 StringVariable: name (10)
 NumericVariable: weight
 DateVariable: date
 DateVariable: time
 DateVariable: datetime
 NumericVariable: commission
 NumericVariable: price

提前致谢 :)

java code-analysis analysis spss spss-modeler
2个回答
1
投票

我尝试重新创建问题并发现了同样的事情。 考虑到该库的许可(请参阅here),我认为这可能是开发人员确保购买许可证的一种方式,因为常规下载仅包含演示版本作为评估(请参阅licensing before the download)。

由于该库相当陈旧(网站的版权是2003-2008,库的要求是Java 1.2,没有泛型,使用了Vector等),我建议使用不同的库,只要你不限于用在你的问题中。

经过快速搜索,结果发现有一个开源spss阅读器here,也可以通过Maven here获得。

使用github页面上的示例,我把它放在一起:

import com.bedatadriven.spss.SpssDataFileReader;
import com.bedatadriven.spss.SpssVariable;

public class SPSSDemo {

    public static void main(String[] args) {
        try {
            SpssDataFileReader reader = new SpssDataFileReader(args[0]);

            for (SpssVariable var : reader.getVariables()) {
                System.out.println(var.getVariableName());
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

我无法找到打印NumericVariable或类似东西的东西,但因为那些是您在问题中使用的库的类名,我将假设那些不是SPSS标准化的。如果是,您将在库中找到类似的内容,或者您​​可以在github页面上打开一个问题。

使用employees.savhere文件,我使用开源库从上面的代码获得了这个输出:

resp_id
gender
first_name
last_name
date_of_birth
education_type
education_years
job_type
experience_years
monthly_income
job_satisfaction

没有额外的角色了!

编辑评论:

那是正确的。我读了一些SPSS的东西,但根据我的理解,只有字符串和数字变量然后以不同的方式格式化。在maven中发布的版本只允许您访问变量的类型代码(说实话,不知道那是什么),但github版本(不幸的是,似乎不会在maven上发布为1.3-SNAPSHOT)和printformat已经介绍。

您可以克隆或下载库并运行mvn clean package(假设您已安装maven)并使用项目中生成的库(在target\spss-reader-1.3-SNAPSHOT.jar下找到)来使用SpssVariable#getPrintFormatSpssVariable#getWriteFormat方法。

那些返回SpssVariableFormat,你可以从中获取更多信息。因为我不知道所有这些是什么,我能做的最好的事情是将你链接到源here,其中对那里实现的东西的引用应该会进一步帮助你(我假设在this link的文档中引用的SpssVariableFormat#getType是可能是最有帮助的,以确定你有什么样的格式。

如果绝对没有用,我想你可以在问题中使用库的演示版本来确定通过it.next().getClass().getSimpleName()的东西,但我只会在没有其他方法来确定格式时求助于此。


1
投票

我不确定,但看着你的代码,it.next()正在返回一个Variable对象。

必须有一些方法被链接到Variable对象,如it.next().getLabel()it.next().getVariableName()。对象上的toString()并不总是有意义的。在SPSSReader库中检查toString()类的Variable方法。

© www.soinside.com 2019 - 2024. All rights reserved.