java - Apache 公用 CSV | 如何在字段中忽略/包含分号、逗号?
问题描述
我正在尝试解析日志文件并将其存储在 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)"
为此,我正在使用Apache commons CSV库。问题是某些字段在特殊字符中具有;
其值,并且它们被解释为分隔符。
例如,如果我们看一下字段 value Mozilla/4.0 (compatible; MSIE 4.01; Windows 95)
。由于;
.
我不知道解决这个问题的理想方法。请看下面,与我使用的库相关的代码快照:
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 printer = new CSVPrinter(writer, CSVFormat.TDF.withHeader(HEADERS));
https://commons.apache.org/proper/commons-csv/apidocs/org/apache/commons/csv/CSVFormat.html#TDF
推荐阅读
- linux - tar 命令的权限
- python-3.x - Project euler #8,错误答案
- javascript - 由于 Google recaptcha - gen_validatorv4.js,frmvalidator 使用“onclick”进行验证
- sql - Postgres:如何在一定时间内连续计算有条件的记录?
- webdriver - webdriver IO 中 Firefox 的 mobileEmulation 功能
- python - PyPDF2在尝试提取第一页时返回空白页
- jquery - jQuery隐藏/显示DIV功能不起作用
- azure - 如何从 Azure Devops macos 代理截取屏幕截图
- javascript - 我在 ckeditor 中使用 find 插件。如何在ckeditor中将替换文本设置为删除线
- apache-kafka - Spring kafka消息反序列化失败,因为content-type设置为application/json