包含许多表的 CSV 文件
csv 文件包含多个表,它可能如下所示:
“垂直表 1” , “id”、“访问次数”、“下载次数” 1、4324、23 2、664、42 3、73、44 4、914、8 《垂直表2》 , “id_of_2nd_tab”、“visits_of_2nd_tab”、“downloads_of_2nd_tab” 1, 524, 3 2、564、52 3、63、84 4、814、8
要读取一张表,我使用 opencsv 中的“HeaderColumnNameTranslateMappingStrategy” 这允许我将 csv 表条目映射到 TableDataBean 对象列表中,如下所示:
HeaderColumnNameTranslateMappingStrategy<TableDataBean> strat = new HeaderColumnNameTranslateMappingStrategy<TableDataBean>();
CSVReader reader = new CSVReader(new FileReader(path), ',');
strat.setType(TableDataBean.class);
Map<String, String> map = new HashMap<String, String>();
map.put("Number of visits", "visits");
map.put("id", "id");
map.put("Number of downloads", "downloads");
strat.setColumnMapping(map);
CsvToBean<TableDataBean> csv = new CsvToBean<TableDataBean>();
List<TableDataBean> list = csv.parse(strat, reader);
这对于第一个表来说效果很好,但是当涉及到第二个表时,值和 属性映射到第一个表的相同属性。的输出
for(TableDataBean bean : list){System.out.println(bean.getVisits());}
如下所示:
<前><代码> 4324 第664章 73 914 无效的 无效的 无效的 第二个选项卡的访问次数 第524章 第564章 63 第814章
我不想将文件拆分为许多文件,每个文件包含一个表。 那么你有什么建议呢?还有其他库支持这种格式吗?
The csv file contains more than one table, it might look like this:
"Vertical Table 1" , "id","visits","downloads" 1, 4324, 23 2, 664, 42 3, 73, 44 4, 914, 8 "Vertical Table 2" , "id_of_2nd_tab","visits_of_2nd_tab","downloads_of_2nd_tab" 1, 524, 3 2, 564, 52 3, 63, 84 4, 814, 8
To read one table I use "HeaderColumnNameTranslateMappingStrategy" from opencsv
which allows me to map the csv-table entries into a List of TableDataBean objects, as seen below:
HeaderColumnNameTranslateMappingStrategy<TableDataBean> strat = new HeaderColumnNameTranslateMappingStrategy<TableDataBean>();
CSVReader reader = new CSVReader(new FileReader(path), ',');
strat.setType(TableDataBean.class);
Map<String, String> map = new HashMap<String, String>();
map.put("Number of visits", "visits");
map.put("id", "id");
map.put("Number of downloads", "downloads");
strat.setColumnMapping(map);
CsvToBean<TableDataBean> csv = new CsvToBean<TableDataBean>();
List<TableDataBean> list = csv.parse(strat, reader);
This works fine for the first table, but when it cames to the second, the values and the attributes are mapped to the same attribute of the first table. The output for
for(TableDataBean bean : list){System.out.println(bean.getVisits());}
would look like this:
4324 664 73 914 null null null visits_of_2nd_tab 524 564 63 814
I don't wanna split the file into many files containing each of them one table.
So what do you suggest ? Is there any other Library that supports this format?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我明白了!我认为reader的类型必须是CSVReader。事实上,我可以为方法 parse 提供继承自 Reader 类的任何对象。
现在,我可以将整个 csv 文件读入字符串,将其拆分,将每个新字符串打包到 StringReader 中,然后将其传递给 parse 方法。
I've got it! I thought that the type of reader have to be of CSVReader. It actually turned out that I can feed the methode parse with any object inheriting from the Reader class.
Now I can read the entire csv-file into a String, splitt it, pack each of the new Strings into a StringReader and than pass it to the parse methode.