使用 open csv 获取 .csv 的内容

发布于 2024-12-12 08:23:33 字数 250 浏览 3 评论 0原文

如何使用 openCSV 获取并显示 csv 的某些行。 我目前有以下代码:

CSVReader reader1 = new CSVReader(new FileReader(mydata_csv.getpath()));
List myDatas = reader1.readAll();

如何显示某一特定行?

也许我可以使用更好的方法来存储我的数据(csv 包含数百行变量)。任何建议都将受到欢迎。

How to get and display some lines of a csv using openCSV.
I currently have the following code :

CSVReader reader1 = new CSVReader(new FileReader(mydata_csv.getpath()));
List myDatas = reader1.readAll();

How to display one specific line ?

Maybe can I use a better way to store my datas (the csv contains lines of hundreds variables). any suggestion would be welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥醉 2024-12-19 08:23:33

opencsv 的文档 http://opencsv.sourceforge.net/#how-to-read 似乎说你的代码返回一个 String[] 列表,

在这种情况下我会这样写:

CSVReader reader1 = new CSVReader(new FileReader(mydata_csv.getpath()));
List<String[]> myDatas = reader1.readAll();
String[] lineI = myDatas.get(i);
for (String[] line : myDatas) {
    for (String value : line) {
        //do stuff with value
    }
}

The documentation for opencsv http://opencsv.sourceforge.net/#how-to-read seems to say that your code returns a list of String[]

in which case I would write it like so:

CSVReader reader1 = new CSVReader(new FileReader(mydata_csv.getpath()));
List<String[]> myDatas = reader1.readAll();
String[] lineI = myDatas.get(i);
for (String[] line : myDatas) {
    for (String value : line) {
        //do stuff with value
    }
}
仅冇旳回忆 2024-12-19 08:23:33

您应该使用以下代码:

CSVReader reader = new CSVReader(new FileReader(mydata_csv.getpath()));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

You should use the following code:

CSVReader reader = new CSVReader(new FileReader(mydata_csv.getpath()));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    // nextLine[] is an array of values from the line
    System.out.println(nextLine[0] + nextLine[1] + "etc...");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文