gpt4 book ai didi

java - 在 Java 中忽略读取 CSV 文件的空值

转载 作者:行者123 更新时间:2023-12-02 10:31:56 26 4
gpt4 key购买 nike

我能够读取我的数据集(csv文件),但是当我运行我的主类时,它显示所有行,包括具有空值的行。有没有办法忽略数据集中带有缺失值(即空值)的每一行?我正在考虑在方法 testNullValue() 中检查这一点,但我真的不知道要检查什么。

我的类(class)

public static BufferedReader exTractTraningData(File datafile, String ListOfCharacteristics) throws IOException {

try {
//create BufferedReader to read csv file
BufferedReader reader = new BufferedReader(new FileReader(datafile));

String strLine = "";
StringTokenizer st = null;

int lineNumber = 0, tokenNumber = 0;;


while ((strLine = reader.readLine()) != null) {
lineNumber++;
//break comma separated line using ","
st = new StringTokenizer(strLine, ",");

while (st.hasMoreTokens()) {
//display csv values
tokenNumber++;
System.out.println("Line # " + lineNumber
+ ", Token : " + st.nextToken(",") );
}
//reset token number
tokenNumber = 0;;
}
} catch (Exception e) {

System.out.println("Exception while reading csv file: " + e);
}
return null;
}

public boolean testNullValue(String ListOfCharacteristics, String ListOfValues){
return false;


}

最后,我不明白为什么我的控制台中的结果没有显示像“name”、“2”、“TV”、“As”、“40”、“10”这样的每一行,而我在这里指定它 st = new StringTokenizer(strLine, ",");

enter image description here

最佳答案

StringTokenizer遇到空值时会忽略空值,并且实际上无法知道它们实际上存在于 CSV 分隔字符串行中,除非标记生成器还提供分隔符作为标记,并且当有两个分隔符标记时,一个接一个,然后是一个空值显然遇到过:

st = new StringTokenizer(strLine, ",", true); 

这是一种在 CSV 文件数据行中检测 null 的真正booger方法,因为现在您必须提供代码来计算两个分隔符标记相继落下的情况,然后完全忽略分隔符标记。这很可能就是为什么没有太多人使用 StringTokenizer 来解析 CSV 文件并且更喜欢使用类似 String#split() 的原因之一。方法,或者更好的是 CSV 解析器 API,如 OpenCSV 。这当然取决于真正需要做什么以及它的范围有多大。

实际上不鼓励在新代码中使用旧的遗留 StringTokenizer 类,因为它的方法不区分标识符、数字和带引号的字符串。类方法甚至无法识别并跳过注释。

无论如何,如果您想要检查任何单个 CSV 行中的任何空值,则无需重新读取该文件。它可以在您当前正在执行的同一单遍读取中完成。这个概念非常简单,利用一种代码机制来读取 CSV 文件数据行,将其分割为 token , token 还维护任何给定行中可能包含的空值,然后进行比较 token 计数与使用 StringTokenizer 计数解析的数据文件行完全相同。这种事情可以在 CSV 数据行标记化后直接完成,例如:

while ((strLine = reader.readLine()) != null) {
// You might want to count lines only if they are valid!
// If so then move this line below the IF statement code
// block.
lineNumber++;
//break comma separated line using ","
st = new StringTokenizer(strLine, ",");

// Is this a blank line OR Is there possibly a null token
// in the data line detected by the String#split() method?
if (st.countTokens() == 0 || (st.countTokens() != strLine.split(",").length)) {
System.out.println("The data line is blank OR there is a null value "
+ "in the data line!");
// Skip this data line from further processing
// within the WHILE loop.
continue;
}

while (st.hasMoreTokens()) {
//display csv values
tokenNumber++;
System.out.println("Line # " + lineNumber
+ ", Token : " + st.nextToken(",") );
}
//reset token number
tokenNumber = 0;
}

我个人只会使用 String#split() 方法,而不是完全使用 StringTokenizer 类,也许像这样:

while ((strLine = reader.readLine()) != null) {
// You might want to count lines only if they are valid!
// If so then move this line below the IF statement code
// block.
lineNumber++;
// Split comma separated line using ","
String[] st = strLine.split(",");
if (st.length == 0 || Arrays.asList(st).contains("")) {
System.out.println("The data line (" + lineNumber + ") is blank OR "
+ "there is a null value in the data line!");
// Skip this data line from further processing
// within the WHILE loop.
continue;
}

StringBuilder sb = new StringBuilder();
sb.append("Line# ").append(lineNumber).append(": ");
for (int i = 0; i < st.length; i++) {
sb.append("Token : ").append(st[i]).
// Ternary Operator used here to add commas
append(i < (st.length-1) ? ", " : "");
}
System.out.println(sb.toString());
}

当然,这一切都假设 CSV 文件数据以逗号分隔,任何分隔符之前或之后都没有空格。当人们发布有关数据文件处理的问题并且没有提供数据在该文件中如何格式化的示例时,这就是问题所在。当然,现在我要解决你的第二个问题,即为什么事情没有按照你想要的方式显示:

And Lastly, I don't why the results in my console is not displaying each rows like this "name", "2 ", "TV ", "As ", " 40", "10"

如果没有示例,谁知道数据如何在文件中呈现以及确切您希望如何在屏幕上呈现。这个例子到底是什么意思,我个人不太明白。此外,不应该是“name”,“gender”,“2”...?我们当然可以猜测,我的猜测是您的分隔符在StringTokenizer<中使用/strong> 方法是错误的,当然,上面的所有示例都是基于您在自己的代码中提供的分隔符。

关于java - 在 Java 中忽略读取 CSV 文件的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53569629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com