Apache CSVParser 不工作
Apache CSVParser is not working
考虑以下代码示例:
private static CSVFormat CSV_FORMAT = CSVFormat.TDF;
logger.debug("Processing record: {}", line);
try {
CSVParser parser = CSVParser.parse(line,CSV_FORMAT);
if(parser.getRecords().isEmpty()) continue;
csvRecord = parser.getRecords().get(0);
} catch (IOException e) {
logger.warn("Skipping line: " + line,e);
continue;
}
出于某种原因,这不是解析。我得到以下输出:
DEBUG:
(OrderParser#parseData:121) - Processing record:
136147091 340834429 4/5/2015 4:35:00
PM 262105109 UFH6285 6 0 0 HWF62 Holmes Humidifier Replacement
Filter 8.99 53.94 0 39.91 0 8.09 0 5.94 5.12 7035997658 Marty Joe aa3003@mail.com Jess
Dude 555 Main st Anywhere CA 900000 1
java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
虽然您可能无法通过 Whosebug 看到它,但我保证那里有标签。但是索引越界异常发生在parser.getRecords().get(0)
如果我调用 parser.getRecordNumber()
,它会报告 1
。不知道是字符编码错误还是什么。
根据要求,这里有一些示例输入夹在 pre 标记内。这里有两行,但我只是发送第二行:
SNumber RecD Receip_ID ListingID Date_Entered 234 ReferenceId QT QT2 QT3 Title P456 Product_Rev Sh2687t Product Shipping Comm ShipFee PerItemFee Tax_Cost Company HoneCode Bill455 Bill788 Email Ship468644 Ship6489 Ship654132158 Ship98198 Ship_To_City Ship_To_State Ship_To_Zip ShippingMethodId
2644 7775 11457 26894 4/9/2015 9:47:00 AM 247520128 700364 1 0 0 Shark Navigator 14.99 14.99 0 11.75 0 2.25 0 0.99 0 84698 Shawn Vanloan 3504620ae7f03@mail.com S Vanloo 166 E Main Rd Anywhere NY 12000 1
根据apidocs,parser.getRecords()
根据给定的格式解析文件,return将内容作为列表。解析位置将在输入的末尾。所以 parser.getRecords()
的另一个调用将 return 一个空列表。
考虑明智地解析输入记录。
Peter Zheng 的回答让我开始了正确的方向。对我来说,解决方案在调用 CSVParser.parse 后出现在代码中。这实际上是 returns 一个可迭代的,所以我进行了以下操作:
CSVParser parser = ... //Any method for obtaining a parser
for(CSVRecord record : parser) {
//Code for operating on the record here
}
考虑以下代码示例:
private static CSVFormat CSV_FORMAT = CSVFormat.TDF;
logger.debug("Processing record: {}", line);
try {
CSVParser parser = CSVParser.parse(line,CSV_FORMAT);
if(parser.getRecords().isEmpty()) continue;
csvRecord = parser.getRecords().get(0);
} catch (IOException e) {
logger.warn("Skipping line: " + line,e);
continue;
}
出于某种原因,这不是解析。我得到以下输出:
DEBUG: (OrderParser#parseData:121) - Processing record: 136147091 340834429 4/5/2015 4:35:00 PM 262105109 UFH6285 6 0 0 HWF62 Holmes Humidifier Replacement Filter 8.99 53.94 0 39.91 0 8.09 0 5.94 5.12 7035997658 Marty Joe aa3003@mail.com Jess Dude 555 Main st Anywhere CA 900000 1 java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
虽然您可能无法通过 Whosebug 看到它,但我保证那里有标签。但是索引越界异常发生在parser.getRecords().get(0)
如果我调用 parser.getRecordNumber()
,它会报告 1
。不知道是字符编码错误还是什么。
根据要求,这里有一些示例输入夹在 pre 标记内。这里有两行,但我只是发送第二行:
SNumber RecD Receip_ID ListingID Date_Entered 234 ReferenceId QT QT2 QT3 Title P456 Product_Rev Sh2687t Product Shipping Comm ShipFee PerItemFee Tax_Cost Company HoneCode Bill455 Bill788 Email Ship468644 Ship6489 Ship654132158 Ship98198 Ship_To_City Ship_To_State Ship_To_Zip ShippingMethodId 2644 7775 11457 26894 4/9/2015 9:47:00 AM 247520128 700364 1 0 0 Shark Navigator 14.99 14.99 0 11.75 0 2.25 0 0.99 0 84698 Shawn Vanloan 3504620ae7f03@mail.com S Vanloo 166 E Main Rd Anywhere NY 12000 1
根据apidocs,parser.getRecords()
根据给定的格式解析文件,return将内容作为列表。解析位置将在输入的末尾。所以 parser.getRecords()
的另一个调用将 return 一个空列表。
考虑明智地解析输入记录。
Peter Zheng 的回答让我开始了正确的方向。对我来说,解决方案在调用 CSVParser.parse 后出现在代码中。这实际上是 returns 一个可迭代的,所以我进行了以下操作:
CSVParser parser = ... //Any method for obtaining a parser
for(CSVRecord record : parser) {
//Code for operating on the record here
}