java csv crlf,CSVParser将LF处理为CRLF

I am trying to parse a CSV file as below

String NEW_LINE_SEPARATOR = "\r\n";

CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);

FileReader fr = new FileReader("201404051539.csv");

CSVParser csvParser = csvFileFormat.withHeader().parse(fr);

List recordsList = csvParser.getRecords();

Now the file got normal lines ending with CRLF characters however for few lines there is additional LF character appearing in middle.

i.e.

a,b,c,dCRLF --line1

e,fLF,g,h,iCRLF --line2

Due to this, the parse operation creates three records whereas actually they are only two.

Is there a way I can get the LF character appearing in middle of second line not treated as line break and get two records only upon parsing?

Thanks

解决方案

I think uniVocity-parsers is the only parser you will find that will work with line endings as you expect.

The equivalent code using univocity-parsers will be:

CsvParserSettings settings = new CsvParserSettings(); //many options here, check the tutorial

settings.getFormat().setLineSeparator("\r\n");

settings.getFormat().setNormalizedNewline('\u0001'); //uses a special character to represent a new record instead of \n.

settings.setNormalizeLineEndingsWithinQuotes(false); //does not replace \r\n by the normalized new line when reading quoted values.

settings.setHeaderExtractionEnabled(true); //extract headers from file

settings.trimValues(false); //does not remove whitespaces around values

CsvParser parser = new CsvParser(settings);

List recordsList = parser.parseAllRecords(new File("201404051539.csv"));

If you define a line separator to be \r\n then this is the ONLY sequence of characters that should identify a new record (when outside quotes). All values can have either \r or \n without being enclosed in quotes because that's NOT the line separator sequence.

When parsing the input sample you gave:

String input = "a,b,c,d\r\ne,f\n,g,h,i\r\n";

parser.parseAll(new StringReader(input));

The result will be:

LINE1 = [a, b, c, d]

LINE2 = [e, f

, g, h, i]

Disclosure: I'm the author of this library. It's open-source and free (Apache 2.0 license)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值