gpt4 book ai didi

java - 如何跳过 Java 中 csv 的第一行?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:09:38 24 4
gpt4 key购买 nike

我想跳过第一行并使用第二行作为标题。

我正在使用 apache commons csv 中的类来处理 CSV 文件。

CSV 文件的标题在第二行,而不是第一行(包含坐标)。

我的代码是这样的:

static void processFile(final File file) {
FileReader filereader = new FileReader(file);
final CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';');
CSVParser parser = new CSVParser(filereader, format);
final List<CSVRecord> records = parser.getRecords();
//stuff
}

我天真地以为,

CSVFormat format = CSVFormat.DEFAULT.withFirstRecordAsHeader().withDelimiter(;)

会解决问题,因为它与 withFirstRowAsHeader 不同,我认为它会检测到第一行不包含任何分号并且不是记录。它没有。我试图跳过第一行(CSVFormat 似乎认为是标题)

CSVFormat format = CSVFormat.DEFAULT.withSkipHeaderRecord().withFirstRecordAsHeader().withDelimiter(;);

但这也行不通。我能做什么? withFirstRowAsHeader 和 withFirstRecordAsHeader 有什么区别?

最佳答案

如果它是标题,则跳过第一行的正确方法是使用不同的 CSVFormat

CSVFormat format = CSVFormat.DEFAULT.withDelimiter(';').withFirstRecordAsHeader();

更新:2022 年 6 月 30 日

对于 1.9+,使用

CSVFormat.DEFAULT.builder()                                                                  
.setDelimiter(';')
.setHeader()
.setSkipHeaderRecord(true) // skip header
.build();

关于java - 如何跳过 Java 中 csv 的第一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45861824/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com