【问题标题】:re-format CSV input file重新格式化 CSV 输入文件
【发布时间】:2010-11-09 16:57:16
【问题描述】:

我想要做的只是最初创建 CSV 文件并在每个月末附加到它。从而生成如下格式的年度CSV文件:

第 1 个月,Nokia8800,156.0
第 1 个月,Nokia3120,113.0
第 2 个月,Nokia8800,16.0
第 2 个月,Nokia3120,152.0
第 3 个月,Nokia8800,44.0
第3个月,Nokia3120、52.0等12个月。

现在我需要用以下文件格式重新格式化这个 CSV: 1、2、3、4、5、6、7、8、9、10、11、12月
诺基亚8800,156.0,16.0,44.0
Nokia3120、113.0、152.0、52.0等12个月。

我怎样才能做到这一点?

提前致谢。

*希望这可以用数组列表解决..

【问题讨论】:

    标签: java csv arraylist


    【解决方案1】:

    您可以使用 PrintWriter 类轻松地附加到 Java 中的文件。创建时,只需使用重载的构造函数告诉 PrintWriter 追加到文件:

    PrintWriter printWriter
        = new PrintWriter(new FileWriter(new File("filename"), true));
    

    至于第二部分,您可以通过将所有内容读入 Map 然后以您想要的格式重写它来做到这一点。

    Map<String, ArrayList<Double>> REGISTRY = new HashMap<String, ArrayList<Double>>();
    REGISTRY.add("Month", new ArrayList<Double>());
    
    Scanner scanner = new Scanner(new File("filename"));
    while (scanner.hashNextLine()) {
        String[] line = scanner.nextLine().split(",");
    
        // for the month key
        String month = line[0].split(" ")[1];
        REGISTRY.get("Month").add(new Double(month));
    
        // then for the rest of the entries
        if (!REGISTRY.containsKey(line[1])) {
            REGISTRY.add(line[1], new ArrayList<Double>());
        }
        REGISTRY.get(line[1]).add(new Double(line[2]));
    }
    

    现在一切都很好并且整理好了,你可以很容易地把它写出来:

    // first the months
    printWriter2.print("Month");
    for (Double d : REGISTRY.get("Month")) {
        printWriter2.print("," + d);
    }
    printWriter2.println();
    
    // now the rest
    for (Map.Entry<String, ArrayList<Double>> tuple : REGISTRY.entrySet()) {
        if (!tuple.getKey().equals("Month")) {
            printWriter2.print(tuple.getKey());
            for (Double d : tuple.getValue()) {
                printWriter2.print("," + d);
            }
            printWriter2.println();
        }
    }
    

    应该是这样的。

    ** 我可能犯了一个错误,此时不在 IDE 面前。请告诉我,以便我更正。

    ** 另外,请注意这不是一个可靠的答案,而是一个非常特定于您输入文件格式的答案。它没有异常处理,这是一件坏事,但你可以充实它。

    【讨论】:

      【解决方案2】:

      最好的解决方案是使用List 和Map。

      由于您只有 12 个月,您可以创建一个 List 并向其中添加 12 个项目,每个月 1 个。

      对于这些项目中的每一个,您可以创建一个Map&lt;String, Double&gt;,其中键是项目的名称,双精度是该项目在该月的值。

      当您需要输出年度报告时,您可以在第一个月循环遍历每个项目,并从其他每个映射中提取该键的值。

      看起来像这样:

      List<Map<String, Double>> list = new ArrayList<Map<String, Double>>();
      for(int i = 0; i < 12; i++)
          list.add(new HashMap<String, Double>());
      
      for(Entry <String, Double> entry : list.get(0).entrySet())
      {
          String row = entry.getKey() + "," + entry.getValue().toString();
          for(Map<String, Double> map : list.subList(1, list.size()))
          {
              row += map.get(entry.getKey()).toString();
          }
      }
      

      【讨论】:

        【解决方案3】:

        只需逐行阅读,将其拆分为, 并将其存储在一个列表中>。 当您阅读它时,只需遍历列表并仅打印第一项。然后再做第二个,依此类推。这样你就可以随心所欲地“转动”你的 csv。

        【讨论】:

          猜你喜欢
          • 2020-10-28
          • 2015-12-07
          • 2018-12-08
          • 1970-01-01
          • 2016-10-04
          • 2016-01-16
          • 1970-01-01
          • 2017-04-19
          • 1970-01-01
          相关资源
          最近更新 更多