gpt4 book ai didi

java - Univocity 解析器 - 处理具有奇怪结构的行

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

我正在尝试找出使用大学解析器处理 CSV 日志文件的最佳方法,其行如下所示,

"23.62.3.74",80,"testUserName",147653,"日志收集设备100","31/02/15 00:05:10 GMT",-1,"10.37.255.3","TCP",“destination_ip=192.62.3.74|product_id=0071|option1_type=(s-dns)|proxy_machine_ip=10.1.255.3”

正如您所看到的,这是一个逗号分隔的文件,但最后一列有一堆以其字段名称为前缀的值。我的要求是从正常字段中捕获值选择性地从这最后一个大领域。

我知道 Univocity 中的主详细信息行处理器,但我怀疑这是否属于该类别。您能引导我走向正确的方向吗?

注意:如果我实现行处理器,我可以处理 rowProcessed(String[] row, ParsingContext context) 中的名称前缀字段,但如果可能的话,我正在寻找 Univocity 原生的内容?

谢谢,

最佳答案

解析器中没有任何本地功能。也许最简单的方法就是使用您提到的 RowProcessor

为了让您的生活更轻松,您可以尝试做的一件事是使用 CsvParser 的另一个实例来解析最后一条记录:

//initialize a parser for the pipe separated bit
CsvParserSettings detailSettings = new CsvParserSettings();
detailSettings.getFormat().setDelimiter('=');
detailSettings.getFormat().setLineSeparator("|");
CsvParser detailParser = new CsvParser(detailSettings);

//here is the content of the last column (assuming you got it from the parser)
String details = "destination_ip=192.62.3.74|product_id=0071|option1_type=(s-dns)|proxy_machine_ip=10.1.255.3";

//The result will be a list of pairs
List<String[]> pairs = detailParser.parseAll(new StringReader(details));

//You can add the pairs to a map
Map<String, String> map = new HashMap<String, String>();
for (String[] pair : pairs) {
map.put(pair[0], pair[1]);
}

//this should print: {destination_ip=192.62.3.74, product_id=0071, proxy_machine_ip=10.1.255.3, option1_type=(s-dns)}
System.out.println(map);

这不会非常快,但如果该输入可以具有随机列名称和与其关联的值,那么至少可以轻松使用 map 。

关于java - Univocity 解析器 - 处理具有奇怪结构的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39585027/

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