[英]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.