简体   繁体   English

在 JAVA 中按列将数据写入/附加到 CSV 文件

[英]Writing/appending data to a CSV file, column wise, in JAVA

I want to write/append data to a CSV file, column-by-column, in below fashion:我想以以下方式将数据逐列写入/附加到 CSV 文件:

    query1       query2       query3
    data_item1   data_item7   data_item12
    data_item2   data_item8   data_item13
    data_item3   data_item9   data_item14
    data_item4   data_item10 
    data_item5   data_item11
    data_item6  

I have the data in a hashMap, with the queryID (ie query1,query2) being the key and data_items for the corresponding queries being the values.我在 hashMap 中有数据,queryID(即 query1,query2)是键,相应查询的 data_items 是值。 The values(data_items for every query) are in a list.值(每个查询的数据项)都在一个列表中。 Therefore, my hash map looks like this :因此,我的哈希图如下所示:

HashMap<String,List<String>> hm = new HashMap<String,List<String>>();

How can I write this data, column by column to a csv, as demonstrated above, using JAVA ?如何使用 JAVA 将这些数据逐列写入 csv,如上所示? I tried CSVWriter, but couldn't do it.我尝试过 CSVWriter,但无法做到。 Can anyone please help me out ?任何人都可以帮我吗?

csv files are mostly used to persist data structured like a table... meaning data with columns and rows that are in a close context. .csv 文件主要用于保存像表格一样结构化的数据……这意味着数据的列和行位于紧密的上下文中。

In your example there seems to be only a very loose connection between query1, 2 and 3, and no connection horizontally between item 1,7 and 12, or 2, 8 and 13 and so on.在您的示例中,query1、2 和 3 之间似乎只有非常松散的连接,并且项目 1,7 和 12 或 2、8 和 13 等之间没有水平连接。

On top of that writing into files are usually facilitated along rows or lines.最重要的是,通常可以沿行或行写入文件。 So you open your file write one line, and then another and so on.所以你打开你的文件写一行,然后是另一行,依此类推。

So to write the data columnwise as you are asking, you have to either restructure your data in your code alrady to have all the data which is written into one line available on writing that line, or run through your csv file and it's lines several times, each time adding another item to a row.因此,要按照您的要求按列写入数据,您必须在代码中重新构建数据,以使写入一行的所有数据在写入该行时可用,或者运行您的 csv 文件及其行数次, 每次将另一个项目添加到一行。 Of course the latter option is very time consuming and would not make much sense.当然,后一种选择非常耗时并且没有多大意义。

So i would suggest if there is really no connection between the data of the 3 queries, you either write your data into 3 different csv files: query1.csv, 2.csv and 3.csv.所以我建议如果 3 个查询的数据之间真的没有联系,你要么将数据写入 3 个不同的 csv 文件:query1.csv、2.csv 和 3.csv。

Or, if you have a horizontal connection ie between item 1,7 and 12, and so on you write it into one csv file, organizing the data into rows and columns.或者,如果您在项目 1,7 和 12 之间有一个水平连接,依此类推,您可以将其写入一个 csv 文件,将数据组织成行和列。 Something like:就像是:

queryNo     columnX     columnY     columnZ
1           item1       item2       item3
2           item7       item8       item9
3           item12      item13      item14 

How to do that is well described in this thread: Java - Writing strings to a CSV file .如何做到这一点在这个线程中有很好的描述: Java - 将字符串写入 CSV 文件 Other examples you can also find here https://mkyong.com/java/how-to-export-data-to-csv-file-java/您还可以在此处找到其他示例https://mkyong.com/java/how-to-export-data-to-csv-file-java/

After days of tinkering around, I finally succeeded.经过几天的折腾,我终于成功了。 Here is the implementation :这是实现:

for(int k=0;k<maxRows;k++) {
            List<String> rowValues  = new ArrayList<String>();
            for(int i=0;i<queryIdListArr.length;i++) {
                subList = qValuesList.subList(i, i+1);
                List<String> subList2 = subList.stream().flatMap(List::stream).collect(Collectors.toList());
                if(subList2.size()<=k) {
                    rowValues.add("");
                }else{
                    rowValues.add(subList2.get(k));
                }

            }
            String[] rowValuesArr = new String[rowValues.size()];
            rowValuesArr = rowValues.toArray(rowValuesArr); 
//          System.out.println(rowValues);
            writer.writeNext(rowValuesArr);
        }

maxRows : Size of the value list with max size. maxRows :具有最大大小的值列表的大小。 I have a list of values for each key.我有每个键的值列表。 My hash map looks like this我的哈希图看起来像这样

 HashMap<String,List<String>> hm = new HashMap<String,List<String>>();

queryIdListArr : List of all the values obtained from the hash map. queryIdListArr :从哈希映射中获得的所有值的列表。

qValuesList : List of all the value lists. qValuesList :所有值列表的列表。

List<List<String>> qValuesList = new ArrayList<List<String>>();

subList2 : sublist obtained from qValuesList using the below syntax : subList2 :使用以下语法从 qValuesList 获得的子列表:

qValuesList.subList(i, i+1);

rowValuesArr is an array that gets populated with the index wise value for each value fetched from qValuesList. rowValuesArr 是一个数组,它填充了从 qValuesList 获取的每个值的索引值。 The idea is to fetch all the values for each index from all the sublists and then write those values to the row.这个想法是从所有子列表中获取每个索引的所有值,然后将这些值写入该行。 If for that index, no value is found, write a blank character.如果该索引没有找到值,则写入一个空白字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM