【发布时间】:2016-12-21 09:00:56
【问题描述】:
我正在使用 apache commons csv 读取从google trends 下载的 CSV 文件中的内容,该文件在右下角的相关查询部分中下载为 csv。文件的一小部分:
Category: All categories
"bluetooth speakers: (1/1/04 - 8/15/16, Worldwide)"
TOP
speaker,100
bluetooth speaker,100
RISING
portable speakers bluetooth,Breakout
portable speakers,Breakout
我要从文件中读取的代码:
private void readCsv(String inputFilePath) {
try {
Reader in = new FileReader(inputFilePath);
Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(in);
for (CSVRecord record : records) {
String topic = record.get(0);
if (topic != null && !topic.isEmpty()) {
System.out.println(topic);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
输出:
bluetooth speakers: (1/1/04 - 8/15/16, Worldwide)
TOP
speaker
bluetooth speaker
RISING
portable speakers bluetooth
portable speakers
期望的输出:
speaker
bluetooth speaker
portable speakers bluetooth
portable speakers
根据来自谷歌的数据(没有标题)和两个标题 TOP 和 RISING 我无法提取所需的值。是否有任何过滤配置我可以应用以获得所需的值?
【问题讨论】:
-
您在一个物理文件中有多个个不同的csv“文件”。在将它们解析为 CSV 之前,您必须将它们分开。
-
@JimGarrison 有图书馆可以做吗?
标签: java csv apache-commons-csv