【问题标题】:Java nested conditional parsing of CSV fileCSV文件的Java嵌套条件解析
【发布时间】:2012-11-05 20:07:22
【问题描述】:

如果我的示例数据是一个如下所示的 CSV 文件:

a,,,b,
36,28,90,5,24

拥有类似的东西的最佳方式是什么

myStringArray = [a, [36,28,90]], [b, [5,24]]

myStringArray1 = {a,b}; // 这部分我可以做
myStringArray2 = {{36,28,90}, {5,24}};

我正在使用基于 Java 的 Processing,但我的问题更多的是关于 Java 的一般功能,所以请原谅我的代码格式,如果它看起来不正确。

这是我的导入类:

class dTable {

  int rowCount;
  int columnCount;
  String[][] data;
  String filename;

  dTable(String _filename) {
    filename = _filename;
  }

  void load() {
    String[] rows = loadStrings(filename);
    String[] columns = split(rows[0], ',');
    rowCount = rows.length;
    columnCount = columns.length;

    //set the size of the matrix
    data = new String[rows.length][columns.length]; 

    // add column pieces into data matrix
    for (int i = 0; i < rows.length; i++) {
      String[] colEntries = split(rows[i], ',');
      for (int j = 0; j < columns.length; j++) {
        data[i][j] = colEntries[j];
      }
    }


  }
}

这是我在解析类中的失败尝试:

class dParse {

  String[] bMaj;
  String[][] bMin;

  dParse() {
  }

  void getList(int integer) {

    ArrayList dMaj = new ArrayList();
    ArrayList dMin = new ArrayList();

    String[][] minimums;

    String[] _rowNameMaj = table.data[0]; //get data first to match
    String[] _rowName = table.data[integer]; // get data to fill

    //get first variables
    for (int i = 0; i<_rowName.length; i++) { //iterate over 
      ArrayList tempMin = new ArrayList();
      if (trim(_rowNameMaj[i]).length() != 0) {
        dMaj.add(_rowNameMaj[i]);
      }
    }

    //place maj values from arraylist into an array
    String[] _bMaj = (String[]) dMaj.toArray(new String[0]);
    bMaj = _bMaj; //set value to global variable


    //min values
    ArrayList bigOne = new ArrayList();
    int count = 0;
    for (int i = 0; i<_rowName.length; i++) { //iterate over 
      ArrayList tempMin = new ArrayList();
      if (trim(_rowNameMaj[i]).length() != 0) { //check if box is not empty & set count to 0
        //tempMin = dMaj.get(i); // set inner list
        //println("maj " + count + " " + _rowNameMaj[i]);
        tempMin.add(_rowName[i]);
        count++;
        //println("min" + count + " " + tempMin);
      }
      else if (trim(_rowNameMaj[i]).length() == 0) { //if next box is empty, add to count
        count++;
        tempMin.add(_rowName[i]);
        //println("min" + count + " ");
      }
      minimums = new String[_bMaj.length][];

     /various unworking attempts below

      //place min values from arraylist into an array
      //String[] temp_bMin = (String[]) tempMin.toArray(new String[0]);
      //fl[] = append((new String(temp_bMin)), fl);

            for (int n = 0; n< bMaj.lenth; n++ ) {
       count[level]mums[n] = (String[]) toArray(new String[0]);
//        println(minimums[n]);
//      }
      tempMin.clear(); //clear temporaryList
    }

  }

  String[] getMaj() {
    return bMaj;
  }

  String[][] getMin() {
    return bMin;
  }
}

任何帮助表示赞赏, 非常感谢, 迪米塔

【问题讨论】:

    标签: java parsing csv import


    【解决方案1】:

    Dimitar... 也许像Commons CSV 这样的库或该页面上其他库的链接会被证明是有用的?但是,如果您坚持使用自己的 CSV 解析器,请查看 java.util.StringTokenizer 类。

    【讨论】:

      【解决方案2】:

      伪代码:

      class Item { 
          String name;
          List<String> values;
      }
      
      String[] names = line1.split(",");
      String[] data  = line2.split(",");
      
      List<Item> items = new ArrayList<Item>();
      Item curItem = null;
      
      for (int i=0; i<data.length, i++) 
      {
          if (names[i].isEmpty())
          {
              if (curItem == null) throw "first name is empty";
          }
          else
          {
              curItem = new Item();
              items.add(curItem);
          }
          curItem.values.add(data[i]);
      }
      

      这使用Item 类来保存每个项目,并使用List&lt;Item&gt; 来保存整个事物。降级以使用嵌套数组作为练习。

      重要如果您需要真正的 CSV 解析(引用字段等),您应该使用 CSV 库进行初始拆分,然后将上述逻辑应用于结果.

      【讨论】:

        猜你喜欢
        • 2020-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-30
        • 2013-02-19
        • 2013-12-16
        • 2016-07-02
        • 2020-08-29
        相关资源
        最近更新 更多