SuperCSV的能力 [英] Abilities of SuperCSV

查看:213
本文介绍了SuperCSV的能力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SuperCSV检查收到的某些文件的内容.该文件的格式是这样的:它具有标头记录,后跟数据记录,后跟CRC32校验和值.

I'm looking at using SuperCSV to check the contents of some files that I receive. The format of the file is such that it has a header record followed by the data records followed by a CRC32 checksum value.

例如

ABC|2|20130115150327|
1|1234567890123456|1234|20130109204710|21130109204710|
2|6543210987654321|1234|20130110043658|21130110043658|
1A345C7D

在这种情况下,我对SuperCSV的功能有一些疑问.

I have a few questions about the capabilities of SuperCSV in this situation.

  • 它是否允许您针对不同的行验证不同的行 定义,即一个用于标题记录,一个用于数据 记录吗?
  • 是否允许您验证定界符(竖线"|" 在这种情况下)必须附加到该行的末尾?
  • 是否存在或已经有人编写了可验证十六进制值的CellProcessor?
  • Does it allow you to validate different lines against different definitions i.e. one for the header record and one for the data records?
  • Does it allow you to validate that the delimiter (pipe '|' in this case) must be appended to the end of the line?
  • Is there, or has anybody written, a CellProcessor that validates hexadecimal values?

推荐答案

  • 它是否允许您针对不同的行验证不同的行 定义,即一个用于标题记录,一个用于数据 记录吗?
  • Does it allow you to validate different lines against different definitions i.e. one for the header record and one for the data records?

是的.通常,您将使用getHeader()读取标题,该标题不使用CellProcessors,但是没有什么阻止您使用CellProcessors使用read()将标题作为正常行读取.每次调用read()时,您都可以传递CellProcessor,因此可以使用3个不同的CellProcessor数组以不同方式处理/验证标头,数据行和校验和.

Yes. Typically, you'd read the header with getHeader(), which doesn't use CellProcessors, but there's nothing stopping you from just reading the header as a normal line with read() using CellProcessors. Each call to read() lets you pass in the CellProcessors, so you can process/validate the header, data rows and checksum differently using 3 different CellProcessor arrays.

  • 是否允许您验证定界符(此位置的竖线"|" 大小写)必须添加到该行的末尾?
  • Does it allow you to validate that the delimiter (pipe '|' in this case) must be appended to the end of the line?

当您使用|作为分隔符时,最后一列将被视为空列(null).这意味着您用于读取标头的CellProcessor数组将必须具有4个元素(对于数据行,则必须具有6个元素),否则您将得到一个异常,指出列数与单元处理器的数量不匹配.通过在末尾放置new Equals(null)处理器,您基本上可以验证该行以|结尾.

As you're using | as the delimiter, then the last column will be treated as an empty column (null). This means that your CellProcessor array for reading the header will have to have 4 elements (or 6 for the data rows), otherwise you will get an exception saying that the number of columns doesn't match the number of cell processors. By putting an new Equals(null) processor at the end, you can essentially validate that the line ends with a |.

  • 是否存在可以验证以下内容的CellProcessor? 十六进制值?
  • Is there, or has anybody written, a CellProcessor that validates hexadecimal values?

您可以使用现有的单元处理器new StrRegex("[0-9A-F]+")来使用正则表达式进行验证.您甚至可以使用

You can use the existing cell processor new StrRegex("[0-9A-F]+") to validate using a regular expression. You can even register a human-readable message for the validation error (e.g. "not a valid hex value") using StrRegex.registerMessage().

如果要将十六进制解析为数字(可能不是,但以防万一),那么Super CSV中不存在ParseHex CellProcessor.如果您编写一个并提交了补丁程序,我将在以后的发行版中包含它!根据数量的多少,最好更新 ParseLong 是否有另一个接受基数的构造函数(在本例中为16)?

If you want to parse the hex as a number (probably not, but just in case), then there's no existing ParseHex CellProcessor in Super CSV. If you write one and submit a patch, I'll include it in the upcoming release! Depending on how big the number is, maybe it's best to update ParseLong to have another constructor that accepts a radix (16 in this case)?

我建议保持简单,并使用 CsvListReader (您可以使用其他阅读器,但是您需要定义一个nameMapping数组以提供标题,数据和校验和行的列名称),如下所示:

I'd recommend keeping things simple and using CsvListReader (you could use the other readers, but you'll need to define a nameMapping array to provide column names for the header, data and checksum rows) as follows:

  1. 使用标题" CellProcessor数组读取第一行(我假设第二列是后续数据行的数量?).

  1. Read the first line (I'm assuming the second column is the number of following data rows?) using the 'header' CellProcessor array.

使用数据" CellProcessor数组读取数据行 n 次(其中 n 由第二个标题列给出).

Read the data rows n times (where n is given by the 2nd header column) using the 'data' CellProcessor array.

使用校验和" CellProcessor数组(可能只是一个ParseChecksum())读取校验和.

Read the checksum using the 'checksum' CellProcessor array (probably just a single ParseChecksum()).

这篇关于SuperCSV的能力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆