gpt4 book ai didi

java - Apache 公共(public) CSV |如何忽略/包括字段中的分号、逗号?

转载 作者:行者123 更新时间:2023-11-30 12:07:23 26 4
gpt4 key购买 nike

我正在尝试解析日志文件并将其存储在 CSV 文件中。这是下面的示例行:

218.1.111.50 - - [13/Mar/2005:10:36:11 -0500] "GET http://www.yahoo.com/ HTTP/1.1" 403 2898 "-" "Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)"

为此,我使用了 Apach commons CSV 库。问题是某些字段在特殊字符 ; 中具有它们的值,并且它们被解释为分隔符。

例如,如果我们查看字段值 Mozilla/4.0(兼容;MSIE 4.01;Windows 95)。由于 ; ,此单个字段被分配给 3 个不同的值。

enter image description here

我不知道解决这个问题的理想方法。请看下面,与我使用的库相关的代码快照:

  CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT
.withHeader(HEADERS));
//
//
Matcher m = p.matcher(line);
Date date=formatter.parse(m.group("Time"));

try {

printer.printRecord(date.getMonth(), date.getDate(), date.getHours(), date.getMinutes(), date.getSeconds(), m.group("NetworkSrcIpv4"),
m.group("ApplicationHttpStatus"),m.group("ApplicationLen"),m.group("ApplicationHttpUserAgent"),
m.group("ApplicationHttpQueryString"));

printer.flush();

} catch (IOException e) {

e.printStackTrace();

}
//

是否有可能自动忽略 ;,或者用一些不会影响预期结果的值替换它们?是否有任何选项可以添加到我的 CSVprinter 中?

感谢您的反馈。

最佳答案

您可以将 TAB 配置为分隔符,而不是使用 DEFAULT 分隔符 -

CSVPrinter 打印机 = new CSVPrinter(writer, CSVFormat.TDF.withHeader(HEADERS));

https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#TDF

关于java - Apache 公共(public) CSV |如何忽略/包括字段中的分号、逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55007263/

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