【问题标题】:Reading the spss file java读取spss文件java
【发布时间】:2019-02-21 09:35:55
【问题描述】:
  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


    【解决方案1】:

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

    由于该库相当旧(网站版权为 2003-2008,对库的要求是 Java 1.2,没有泛型,使用向量等),只要您不受限制,我会推荐不同的库到您问题中使用的那个。

    经过快速搜索,发现有一个开源的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 页面上打开问题。

    使用来自hereemployees.sav 文件,我使用开源库从上面的代码中得到了这个输出:

    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 版本(确实 not 似乎在 maven 上发布为 1.3- SNAPSHOT 不幸的是)在 write- 和 printformat 被引入之后。

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

    这些返回一个SpssVariableFormat,您可以从中获取更多信息。因为我不知道这一切是关于什么的,所以我能做的最好的就是将您链接到源代码 here 那里对在那里实现的东西的引用应该可以进一步帮助您(我假设 this linkSpssVariableFormat#getType 的文档可能最有助于确定您拥有哪种格式。

    如果绝对没有用,我想您也可以使用问题中库的演示版本来通过it.next().getClass().getSimpleName() 确定内容,但只有在没有其他方法来确定格式。

    【讨论】:

    • 我使用了你要求我使用的东西。但它只返回两种类型:String 或 Double。现在我无法找到给定的值是“日期”还是某个双精度值。
    • 是的,你完全正确。那个新罐子可以完成这项工作。 :)
    【解决方案2】:

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

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

    【讨论】:

    • javadoc 中查找SPSSReader 并找到答案非常简单——在我看来。 Mohamed 为您提供了一些可用的方法,例如 getLabel()
    • 我检查了你的要求。 toString() 返回 this.getClassName() + ":" + this.getName() + "(" + this.getLabel() + ")"。即使我删除 this.getLabel(),它也会打印“nameogr”而不是“name”。
    • @Mohamed Anees A,你能帮我找到吗?
    猜你喜欢
    • 2013-05-19
    • 2011-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多