简体   繁体   English

如何将 CSV 文件转换为列表<Map<String,String> &gt;

[英]How to convert a CSV file to List<Map<String,String>>

I have a CSV file which has a header in the first line.我有一个 CSV 文件,第一行有一个标题。 I want to convert it to List<Map<String, String>> , where each Map<String, String> in the list represents a record in the file.我想将它转换为List<Map<String, String>> ,其中List<Map<String, String>>中的每个Map<String, String>代表文件中的一条记录。 The key of the map is the header and the value is the actual value of the field.映射的键是标题,值是字段的实际值。 What I have so far:到目前为止我所拥有的:

BufferedReader br = <handle to file>;
// Get the headers to build the map.
String[] headers = br.lines().limit(1).collect(Collectors.toArray(size -> new String[size]));
Stream<String> recordStream = br.lines().skip(1);

What further operations can I perform on recordStream so that I can transform it to List<Map<String, String>> ?我可以对recordStream执行哪些进一步的操作,以便我可以将其转换为List<Map<String, String>>

Sample CSV file is:示例 CSV 文件是:

header1,header2,header3   ---- Header line
field11,field12,field13   ----> need to transform to Map where entry would be like header1:field11 header2:field12 and so on.
field21,field22,field23
field31,field32,field33

Finally all these Maps need to be collected to a List.最后,需要将所有这些 Map 收集到一个列表中。

The following will work.以下将起作用。 The header line is retrieved by calling readLine directly on the BufferedReader and by splitting around , .标题行是通过直接在BufferedReader上调用readLine并围绕,拆分来检索的。 Then, the rest of the file is read: each line is split around , and mapped to a Map with the corresponding header.然后,读取文件的其余部分:每一行都围绕 分割,并映射到具有相应标题的Map

try (BufferedReader br = new BufferedReader(...)) {
    String[] headers = br.readLine().split(",");
    List<Map<String, String>> records = 
            br.lines().map(s -> s.split(","))
                      .map(t -> IntStream.range(0, t.length)
                                         .boxed()
                                         .collect(toMap(i -> headers[i], i -> t[i])))
                      .collect(toList());
    System.out.println(headers);
    System.out.println(records);
};

A very important note here is that BufferedReader.lines() does not return a fresh Stream when it is called: we must not skip 1 line after we read the header since the reader will already have advanced to the next line.这里非常重要的一点是, BufferedReader.lines()在被调用时不会返回一个新的Stream :我们不能在读取标题后跳过 1 行,因为读取器已经前进到下一行。

As a side note, I used a try-with-resources construct so that the BufferedReader can be properly closed.作为旁注,我使用了try-with-resources构造,以便可以正确关闭BufferedReader

I know this is a bit of an old question, but I ran into the same problem, and created a quick sample of the Commons CSV solution mentioned by Tagir Valeev :我知道这是一个老问题,但我遇到了同样的问题,并创建了Tagir Valeev提到的Commons CSV解决方案的快速示例:

            Reader in = new FileReader("path/to/file.csv");
            Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in);

            List<Map> listOfMaps = new ArrayList<>();
            for (CSVRecord record : records) {
                listOfMaps.add(record.toMap());
            }

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

相关问题 如何转换列表<String>列表到 csv 字符串 - How to convert a List<String> list to csv string 如何转换地图 <String, List<String> &gt;到地图 <String, String> 在java 8中 - How to Convert a Map<String, List<String>> to Map<String, String> in java 8 如何转换列表<map<string, object> > 列出<map<string, string> > </map<string,></map<string,> - How to Convert List<Map<String, Object>> to List<Map<String, String>> 如何将单片 CSV 字符串转换为列表<string> ?</string> - How to convert a monolithic CSV String into a List<String>? Java - 如何将 CSV 文件读入 Map <string, map<string, string> ></string,> - Java - How to read a CSV file into a Map<String, Map<String, String>> 如何将java对象列表转换为Map <String, Map<String, String> &gt; - How to convert a list of java objects to Map<String, Map<String, String>> 如何转换列表<map<string,object> > 至 Map<string, string> ? </string,></map<string,object> - How to convert List<Map<String,Object>> to Map<String, String>? 如何转换列表<map<string, string> &gt; 列出<map<string, map<string, string> &gt;&gt; </map<string,></map<string,> - how to convert List<Map<String, String>> to List<Map<String, Map<String, String>>> 如何转换列表<String>到地图<String,List<String> &gt; 基于分隔符 - How to convert List<String> to Map<String,List<String>> based on a delimeter 将字符串转换为 Map 列表 - Convert String to List of Map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM