我在用RapidMiner导入CSV文件时遇到了问题。浮点值是用逗号编写的,而不是整数和小数值之间的分离点。

有人知道如何以这种方式正确导入格式的值?

样本数据:

BMI;1;0;1;1;1;blue;-0,138812155;0,520378909;5;0;50;107;0;9;0;other;good;2011 BMI;1;0;1;1;1;pink;-0,624654696;;8;0;73;120;1;3;0,882638889;other;good;2011

快速矿工实际上将其解释为“多项式”。强迫它“真实”仅导致对“ 0”值的正确解释。

谢谢

有帮助吗?

解决方案

使用半彩色作为定界符。您可以使用 java.util.Scanner 阅读每行。 String.split() 在半彩色上分裂。当您获得逗号的令牌时,您可以使用 String.replace() 将逗号更改为小数。那你可以使用 Float.parseFloat()

希望这可以回答您的疑问。

其他提示

这似乎是一个非常古老的要求。不确定这是否会对您有所帮助,但这可能会对其他情况有所帮助。

步骤1:在“读取CSV”运算符中,在“导入配置向导”下,请确保选择“ semicolon”作为分隔符

步骤2:使用“猜测类型”操作员。属性过滤器类型 - >子集,选择属性 - >选择属性8、9和16(基于上面的示例),将“小数点字符”更改为“”,您应该全部设置。

希望这有帮助(某人!)

public static void main(String args){
    BufferedReader br = new BufferedReader(new FileReader("c:\\path\\semicolons and numbers and commas.csv"));
    try {
        for(String line; (line=br.readLine()) != null);) {
            //Variable line now has a single line from the file. This code will execute for each line.
            String array = line.split(";");// Split on the semicolon. Beware of changing this. This uses regex which means that some characters mean something like . means anything, not just dots.
            double firstDouble = Double.parseDouble(array[7].replace(',','.')); // Get field 7 (the eighth field) and turn it into a double (high precision floating point). Replace , with . so it will not make an error
            System.err.println("Have a number " + firstDouble);
            System.err.println("Can play with it " + (firstDouble * 2.0));
        }
    }finally{
        br.close(); // Free resources (and unlock file on Windows).
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top