小编典典

有没有一种简单的方法来输出列式CSV?

algorithm

我正在尝试将长度不定的多个数据列表输出到CSV文件。每个列表应该是输出CSV文件中的一列。有直接的做事方法吗?如果我将每个列表都作为一行输出,那么我将遍历每个列表并在结束时输出一个返回值,但是这种方法在按列工作时不起作用。

我曾想过一次逐项检查所有列表并增加一个计数器,但这也会失败,因为某些列表比其他列表长。为了解决这个问题,我将不得不在每次迭代中检查计数器是否在每个列表的末尾,这在计算方面将是相当昂贵的。

感谢您的任何想法!


阅读 280

收藏
2020-07-28

共1个答案

小编典典

我认为这很简单:

public static void main(String... args) throws IOException {

    ArrayList<ArrayList<String>> rows = getRandomData();

    if (rows.size() == 0)
        throw new RuntimeException("No rows");

    // normalize data
    int longest = 0;
    for (List<String> row : rows)
        if (row.size() > longest)
            longest = row.size();

    for (List<String> row : rows)
        while (row.size() < longest)
            row.add("");

    if (longest == 0)
        throw new RuntimeException("No colums");

    // fix special characters
    for (int i = 0; i < rows.size(); i++)
        for (int j = 0; j < rows.get(i).size(); j++)
            rows.get(i).set(j, fixSpecial(rows.get(i).get(j)));

    // get the maximum size of one column
    int[] maxColumn = new int[rows.get(0).size()];

    for (int i = 0; i < rows.size(); i++)
        for (int j = 0; j < rows.get(i).size(); j++)
            if (maxColumn[j] < rows.get(i).get(j).length())
                maxColumn[j] = rows.get(i).get(j).length();

    // create the format string
    String outFormat = "";
    for (int max : maxColumn)
        outFormat += "%-" + (max + 1) + "s, ";
    outFormat = outFormat.substring(0, outFormat.length() - 2) + "\n";

    // print the data
    for (List<String> row : rows)
        System.out.printf(outFormat, row.toArray());

}

private static String fixSpecial(String s) {

    s = s.replaceAll("(\")", "$1$1");

    if (s.contains("\n") || s.contains(",") || s.contains("\"") || 
            s.trim().length() < s.length()) {
        s = "\"" + s + "\"";
    }

    return s;
}

private static ArrayList<ArrayList<String>> getRandomData() {

    ArrayList<ArrayList<String>> data = new ArrayList<ArrayList<String>>();

    String[] rand = { "Do", "Re", "Song", "David", "Test", "4", "Hohjoh", "a \"h\" o", "tjo,ad" };
    Random r = new Random(5);

    for (int i = 0; i < 10; i++) {

        ArrayList<String> row = new ArrayList<String>();

        for (int j = 0; j < r.nextInt(10); j++)
            row.add(rand[r.nextInt(rand.length)]);

        data.add(row);
    }

    return data;
}

输出(因为它是随机的,所以很难看)(转义):

Re       , 4           , "tjo,ad" , "tjo,ad" ,    
"tjo,ad" , "a ""h"" o" ,          ,          ,    
Re       , "a ""h"" o" , Hohjoh   , "tjo,ad" , 4  
4        , David       ,          ,          ,    
4        , Test        , "tjo,ad" , Hohjoh   , Re 
Do       , Hohjoh      , Test     ,          ,    
Hohjoh   , Song        ,          ,          ,    
4        , Song        ,          ,          ,    
4        , Do          , Song     , Do       ,    
Song     , Test        , Test     ,          ,
2020-07-28