Java - 根据列将CSV解析为Hashmap和Arraylist [英] Java - parse CSV into Hashmap and Arraylist based on columns

查看:843
本文介绍了Java - 根据列将CSV解析为Hashmap和Arraylist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要求这个任务的帮助:我有CSV这样的例子:

  column1 $ column2 $第3栏
123 $ xyz $ 321
456 $ zyx $ 654

我想以Java解析它,所以我会为每列提供一个Array列表的Hashmap。
例如:

  [column1,[123,456]] 
[column2,[ xyz,zyx]]
[column3,[321,654]]



有人已经给了我建议如何通过数组列表来解决这个任务,但是我想使用Hashmap,所以我可以使用列的索引。

  public static void main(String [] args){
ArrayList< ArrayList<串GT;> columns = new ArrayList< ArrayList< String>>();
BufferedReader br = null;
尝试{
String sCurrentLine;
br = new BufferedReader(new FileReader(testing.cvs)); ((sCurrentLine = br.readLine())!= null){
String [] fields = sCurrentLine.split(\\ $);


for(int i = 0; i< fields.length; i ++){
if(columns.size()<= i){
columns.add(new ArrayList< String> ;());
}
columns.get(i).add(fields [i]);
}
}

} catch(IOException e){
e.printStackTrace();
} finally {
try {
if(br!= null)br.close();
} catch(IOException ex){
ex.printStackTrace();
}
}
}

谢谢大家

解决方案

使用 univocity

  CsvParserSettings parserSettings = new CsvParserSettings(); 
parserSettings.getFormat()。setLineSeparator(\\\
);
parserSettings.getFormat()。setDelimiter('$');
parserSettings.setHeaderExtractionEnabled(true);

//要获取所有列的值,请使用列处理器
ColumnProcessor rowProcessor = new ColumnProcessor();
parserSettings.setRowProcessor(rowProcessor);

CsvParser parser = new CsvParser(parserSettings);

//这将在我们的列处理器
parser.parse(new FileReader(testing.cvs));

//最后,我们可以得到列值:
Map< String,List< String>> columnValues = rowProcessor.getColumnValuesAsMapOfNames();

清洁,快速和轻松比手工完成。这将处理不同列数的行,并将 null 添加到列表中,而不是向您抛出异常。



披露:我是这个图书馆的作者。它是开放源代码和免费的(Apache V2.0许可证)。

I would like to ask for help with this task: I have CSV for example like this:

column1$column2$column3
123$xyz$321
456$zyx$654

And I would like to parse it by Java, so I would have a Hashmap of Array lists for every column. For example:

["column1",[123,456]]
["column2",[xyz,zyx]]
["column3",[321,654]]

Thanks everyone.

Someone already gave me advice how to solve this task by array lists of array lists, but I would like to use the Hashmap, so I could have the index of the column. How could I edit this code?

public static void main(String[] args) {
    ArrayList<ArrayList<String>> columns = new ArrayList<ArrayList<String>>();
    BufferedReader br = null;
    try {
        String sCurrentLine;
        br = new BufferedReader(new FileReader("testing.cvs"));

        while ((sCurrentLine = br.readLine()) != null) {
            String[] fields = sCurrentLine.split("\\$");
            for (int i = 0; i < fields.length; i++) {
                if (columns.size()<=i){
                    columns.add(new ArrayList<String>());
                }
                columns.get(i).add(fields[i]);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

Thank everyone

解决方案

Use univocity-parsers' ColumnProcessor to do this for you reliably:

CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.getFormat().setLineSeparator("\n");
parserSettings.getFormat().setDelimiter('$');
parserSettings.setHeaderExtractionEnabled(true);

// To get the values of all columns, use a column processor
ColumnProcessor rowProcessor = new ColumnProcessor();
parserSettings.setRowProcessor(rowProcessor);

CsvParser parser = new CsvParser(parserSettings);

//This will kick in our column processor
parser.parse(new FileReader("testing.cvs"));

//Finally, we can get the column values:
Map<String, List<String>> columnValues = rowProcessor.getColumnValuesAsMapOfNames();

Clean, faster and easiers than doing it by hand. This will handle situations such as rows with different number of columns and add null to the list instead of throwing exceptions at you.

Disclosure: I am the author of this library. It's open-source and free (Apache V2.0 license).

这篇关于Java - 根据列将CSV解析为Hashmap和Arraylist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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