【问题标题】:How to remove some columns from a dataset?如何从数据集中删除一些列?
【发布时间】:2016-11-01 06:50:01
【问题描述】:

我有一个带有文本文件(txt 格式)的大型数据集。 文本文件包含这种格式的数据:

Name, Number, Timestamp, Sensordata1, Sensordata2, ... , Sensordata40
Name, Number, Timestamp, Sensordata1, Sensordata2, ... , Sensordata40
Name, Number, Timestamp, Sensordata1, Sensordata2, ... , Sensordata40

现在我需要从每一行中删除数字和时间戳。

我现在的代码:

try{
            // Open the file that is the first
            // command line parameter

            FileInputStream fstream = new FileInputStream("file.txt");

            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {

                // Print the content on the console
                System.out.println (strLine);
            }

            //Close the input stream
            in.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }

如何在 Java 中做到这一点?

【问题讨论】:

  • 类似 String.split(',') 的东西,获取一个数组并删除你需要的东西

标签: java text-files bufferedreader


【解决方案1】:

有几种方法可以完成此操作,具体取决于您希望花费多长时间检测列等内容,最简单的方法是静态输入您要在示例编号 1 和 2 中删除的与数组相关的列,这可以在您的示例中像这样完成:

package stackquestions;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

 public class StackQuestions {


    public static void main(String[] args) {
       try{
            // Open the file that is the first
            // command line parameter

            FileInputStream fstream = new FileInputStream("file.txt");

            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;

            //Read File Line By Line
            while ((strLine = br.readLine()) != null)   {
                String[] data=strLine.split(",");


                for(int i=0;i<data.length;i++){
                    if(i!=1 && i!=2){
                         System.out.println (data[i]);
                    }

                }
                // Print the content on the console

            }

            //Close the input stream
            in.close();
        }catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
}
}

另一种方法是根据正在读取的行是否是第一行来检测列,拆分第一行(假设正在读取的第一行包含标题,然后在每次迭代时检查索引以查看哪一列数据所属。

【讨论】:

  • 另请注意,由于您使用的是逗号分隔的文件,如果您致力于制作更稳定的程序,还有其他方法可以更正式地读取该文件的结构:[链接] (mkyong.com/java/how-to-read-and-parse-csv-file-in-java)。这种方法还可以更灵活地更改在以后的数据中删除哪些列,而无需引用静态输入的数字,例如 1 和 2。无论哪种方式,希望它有所帮助,祝你好运:)
  • 很好的参考!非常感谢!
【解决方案2】:

如果相同的值总是出现在同一列中,那么我相信您可以将所有值添加到ArrayList,循环删除您不需要的值,然后将其写入返回文件。

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2021-09-20
    • 1970-01-01
    • 2011-10-29
    • 2015-08-23
    相关资源
    最近更新 更多