简体   繁体   English

CSV数据生成降序

[英]CSV data generate descending sequence

I have a CSV file to read and the sample data is below: 我有一个CSV文件可供读取,示例数据如下:

SEQ     NAME    GROUP
 1      MARK      A
 2      KEVIN     A
 3      JOE       A
 1      MARY      B
 2      JANE      B
 3      ANN       B
 4      MAY       B

I can't find an easy way to do it in Java that will read the above CSV file and add another column which is just a descending sequence like the REV column: 我找不到在Java中执行此操作的简单方法,该方法将读取上面的CSV文件并添加另一列,该列只是一个降序序列,例如REV列:

SEQ     REV     NAME   GROUP
1       3       MARK    A
2       2       KEVIN   A
3       1       JOE     A
1       4       MARY    B
2       3       JANE    B
3       2       ANN     B
4       1       MAY     B

I know how to use a buffered reader or a scanner but don't know how to create a reverse seq based on group column since you have to know the maximum length for each group. 我知道如何使用缓冲读取器或扫描仪,但不知道如何根据组列创建反向序列,因为您必须知道每个组的最大长度。

Any ideas please? 有什么想法吗?

Update: Sorry but I have to use plain java without any library as we are working with a legacy system. 更新:很抱歉,但是在使用旧系统时,我必须使用不带任何库的纯Java。

You can try using supercsv , which will give you map of column value map(check for method CsvMapReader) then you can iterate and group it according to your requirement. 您可以尝试使用supercsv ,它将为您提供列值映射的映射(检查方法CsvMapReader),然后可以根据需要对其进行迭代和分组。

Hope this helps 希望这可以帮助

If you are reading one line, you don't know the group length for the current group. 如果您要读一行,则不知道当前组的组长度。 So split it off into two passes. 因此,将其分为两个阶段。

Pass 1 is only to determine the group length for each group. 通道1仅用于确定每个组的组长度。 You can store it in a Map<String, Integer> with the group name as key and the (so far) maximum group length. 您可以将其存储在Map<String, Integer>并以组名作为键,以及(到目前为止)最大组长。 For each line you have to look, if there is already an entry for that group. 对于每行,您都必须查看该组是否已有条目。 If yes, increment the value. 如果是,则增加该值。 If not store 1 for this group. 如果不是,则为该组存储1。

Then in Pass 2 you read the file line by line a second time. 然后,在Pass 2中,您第二逐行读取文件。 But with the above map, you already know the maximum group length for the current group. 但是使用上面的地图,您已经知道当前组的最大组长度。

If the file is too big to store all groups in memory then you should store them in a file, eg with a RandomAccessFile . 如果文件太大而无法将所有组存储在内存中,则应将它们存储在文件中,例如使用RandomAccessFile But it is easier to increase the memory. 但是增加内存比较容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM