我使用apache commons csv來讀取一個CSV文件,我從google trends下載爲相關查詢區右下方有一個CSV獲取內容。該文件的一小部分:的Java讀取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和瑞星我無法提取所需的值。是否有任何配置過濾我可以申請獲得所需的值?
你有什麼有*** ***多在一個物理文件不同的CSV「文件」。在將它們解析爲CSV之前,必須將它們分開。 –
@JimGarrison有沒有可以做的圖書館? –