如何在读取CSV文件时分隔换行?

huangapple go评论91阅读模式
英文:

How to delimit new line when reading CSV file?

问题

// 在列表类中

public void load() throws FileNotFoundException
{
    Scanner input = new Scanner(new FileReader("a_file.csv"));

    object to_add; // 要添加到列表中的对象

    input.useDelimiter(",|\\n|\\r");

    while (input.hasNext())
    {
        String n = input.next(); // 在第一个循环运行后,这个数据获得了值 ""
        String l = input.next(); // 在第二次循环中,这个成员获得了 n 应该得到的数据 "stringD"
        String d = input.next(); // 这个获得了 "stringE"
        int a = input.nextInt(); // 这个尝试获取 "stringF",导致程序崩溃
        int b = input.nextInt();
        int c = input.nextInt();

        to_add = new object(n, l, d, a, b, c); // 调用拷贝构造函数填充数据成员

        insert(to_add); // 将对象插入列表中
    }

    input.close();
}

请注意,您的代码中有一些问题。在第二次迭代时,您的代码尝试将字符串转换为整数,但实际上它应该是另一个字符串。此外,代码中的 "b" 变量没有在代码片段中定义,您可能需要根据您的代码进行相应的更改。

英文:

I am trying to read a file where each line has data members, separated by commas, that are meant to populate an object's data members, I tried using the regex "|" symbol to separate "," and "\n" along with "\r" for getting to the new line. However, after reading the first line, the first data member of the second line does not get read right away but rather a "" character gets read beforehand. Am I using the wrong regex symbols? or am I not using the right approach? I read that there are many ways to tackle this and opted to use scanner since seemed the most simple, using the buffer reader seemed very confusing since it seems like it returns arrays and not individual strings and ints which is I'm trying to get.

The CSV file looks something like this

stringA,stringB,stringC,1,2,3
stringD,stringE,stringF,4,5,6
stringG,stringH,stringI,7,8,9

My code looks something like this

//In list class

public void load() throws FileNotFoundException
    {
        Scanner input = new Scanner(new FileReader("a_file.csv"));

        object to_add; //To be added to the list

        input.useDelimiter(",|\\n|\\r");

        while (input.hasNext())
        {
            String n = input.next(); //After the first loop run, this data gets the value ""
            String l = input.next(); //During this second run, this member gets the data that n was supposed to get, "stringD"
            String d = input.next(); //This one gets "stringE"
            int a = input.nextInt(); //And this one tries to get "stringF", which makes it crash
            int c = input.nextInt();

            to_add = new object(n, l, d, a, b, c); //Calling copy constructor to populate data members

            insert(to_add); //Inserting object to the list
        }

        input.close();
    }

答案1

得分: 1

使用Apache Commons CSV。这是用户指南链接:https://commons.apache.org/proper/commons-csv/user-guide.html

英文:

Use Apache Commons CSV. Here is the user guide https://commons.apache.org/proper/commons-csv/user-guide.html

答案2

得分: 0

你可以使用OpenCSV来完成这个任务,以下是一个关于如何使用这个库的教程。你可以从Maven仓库下载这个库。

所以以下是你需要执行的代码:

Reader reader = Files.newBufferedReader(Paths.get("path/to/csvfile.csv"));
CSVReader csvReader = new CSVReader(reader);
List<String[]> dataList = new ArrayList<>();
dataList = csvReader.readAll();

reader.close();
csvReader.close();

Object to_add;

for (String[] rowData : dataList) {
    String textOne = rowData[0];
    String textTwo = rowData[1];
    String textThree = rowData[2];
    int numberOne = Integer.parseInt(rowData[3]);
    int numberTwo = Integer.parseInt(rowData[4]);
    int numberThree = Integer.parseInt(rowData[5]);

    to_add = new Object(textOne, textTwo, textThree, numberOne, numberTwo, numberThree);

    insert(to_add);
}
英文:

You can do this with OpenCSV and here is a tutorial how to use this library. You can download the library from the Maven Repository.

So following is the code what you need to do,

Reader reader = Files.newBufferedReader(Paths.get(&quot;path/to/csvfile.csv&quot;));
CSVReader csvReader = new CSVReader(reader);
List&lt;String[]&gt; dataList = new ArrayList&lt;&gt;();
dataList = csvReader.readAll();

reader.close();
csvReader.close();

Object to_add;

for (String[] rowData : dataList) {
    String textOne = rowData[0];
    String textTwo = rowData[1];
    String textThree = rowData[2];
    int numberOne = Integer.parseInt(rowData[3]);
    int numberTwo = Integer.parseInt(rowData[4]);
    int numberThree = Integer.parseInt(rowData[5]);

    to_add = new Object(textOne, textTwo, textThree, numberOne, numberTwo, numberThree);

    insert(to_add);
}

huangapple
  • 本文由 发表于 2020年8月23日 16:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63544972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定