0

我有一个 csv 编码某些项目的词库,并且预期每行的条目数对于不同的行是不同的。

第一行包含 25 个标记/同义词。其余的线较少。但是String[]被读取的长度都是 25。较短的行用空字符串填充。

有什么办法可以避免这种情况发生吗?

我的代码如下所示:

CSVReader reader = new CSVReader(new FileReader("thesaurus.csv", '\t'));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
    System.out.println("length of the row: "+ nextLine.length);
}

来自 csv 的示例行:

search  examination  exploration    hunt    inquiry inspection  investigation   pursuit quest   research    chase   frisking    going-over  inquest pursual pursuance   pursuing    rummage scrutiny    shakedown   fishing expedition  legwork perquisition    wild-goose chase    witch hunt
school  schule
saint   st. st

当我一个接一个地打印 String[] 项目时,我得到了这个:

'school', 'schule', , , , , , , , , , , , , , , , , , , , , , , , 
'saint', 'st.', 'st', , , , , , , , , , , , , , , , , , , , , , , 
4

1 回答 1

0

我猜你的输入 csv 中有尾随空格。

String[] nextLine;
    while ((nextLine = reader.readNext()) != null) {
        for (String line : nextLine) {
            System.out.println(line.replaceAll(" ", "."));
        }
    }

如果没有尾随空格,这是输出。

search..examination..exploration....hunt....inquiry.inspection..investigation...pursuit.quest...research....chase...frisking....going-over..inquest.pursual.pursuance...pursuing....rummage.scrutiny....shakedown...fishing.expedition..legwork.perquisition....wild-goose.chase....witch.hunt
school..schule
saint...st..st

如果有尾随空格,这是输出。

search..examination..exploration....hunt....inquiry.inspection..investigation...pursuit.quest...research....chase...frisking....going-over..inquest.pursual.pursuance...pursuing....rummage.scrutiny....shakedown...fishing.expedition..legwork.perquisition....wild-goose.chase....witch.hunt
school..schule............................
saint...st..st.............................................
于 2013-08-29T05:00:03.240 回答