逐列读取 CSV 文件

2022-09-01 16:33:22

我想从多列csv文件中读取特定列,并使用Java在其他csv文件中打印这些列。请帮忙吗?以下是我的代码,用于逐行打印每个令牌。但我希望从多列csv中打印出几列。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;

public class ParseCSV {

    public static void main(String[] args) {

        try
        {

            //csv file containing data
            String strFile = "C:\\Users\\rsaluja\\CMS_Evaluation\\Drupal_12_08_27.csv";

            //create BufferedReader to read csv file
            BufferedReader br = new BufferedReader( new FileReader(strFile));
            String strLine = "";
            StringTokenizer st = null;
            int lineNumber = 0, tokenNumber = 0;

            //read comma separated file line by line
            while( (strLine = br.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 # " + tokenNumber
                                + ", Token : "+ st.nextToken());


                            System.out.println(cols[4]);

答案 1

您应该使用出色的OpenCSV来读取和写入CSV文件。要调整您的示例以使用该库,它看起来像这样:

public class ParseCSV {
  public static void main(String[] args) {
    try {
      //csv file containing data
      String strFile = "C:/Users/rsaluja/CMS_Evaluation/Drupal_12_08_27.csv";
      CSVReader reader = new CSVReader(new FileReader(strFile));
      String [] nextLine;
      int lineNumber = 0;
      while ((nextLine = reader.readNext()) != null) {
        lineNumber++;
        System.out.println("Line # " + lineNumber);

        // nextLine[] is an array of values from the line
        System.out.println(nextLine[4] + "etc...");
      }
    }
  }
}

答案 2

非常简单的方式读取CSV文件,这在Java中很常见。您实际上不需要加载任何额外的第三方库来为您执行此操作。CSV(逗号分隔值)文件只是一个普通的纯文本文件,将数据逐列存储,然后用分隔符(例如逗号“,”)拆分。

为了从 CSV 文件中读取特定列,有几种方法。最简单的方法如下:

无需任何第三方库即可读取 CSV 的代码

BufferedReader br = new BufferedReader(new FileReader(csvFile));
while ((line = br.readLine()) != null) {
    // use comma as separator
    String[] cols = line.split(cvsSplitBy);
    System.out.println("Coulmn 4= " + cols[4] + " , Column 5=" + cols[5]);
}

如果您注意到,这里没有特别之处。它只是读取一个文本文件,并通过分隔符吐出它 - “,”。

考虑从GeoLite免费下载数据库中的旧国家/地区CSV数据中提取

"1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
"1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
"1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
"1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
"1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
"1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
"1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
"1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"

以上代码将输出如下:

Column 4= "AU" , Column 5="Australia"
Column 4= "CN" , Column 5="China"
Column 4= "AU" , Column 5="Australia"
Column 4= "CN" , Column 5="China"
Column 4= "JP" , Column 5="Japan"
Column 4= "CN" , Column 5="China"
Column 4= "JP" , Column 5="Japan"
Column 4= "TH" , Column 5="Thailand"

实际上,您可以获取 a 中的列,然后只需使用 .putMapkey

希希尔