繁体   English   中英

将CSV文件读取到哈希图中

[英]reading CSV file into hashmap

我有一个CSV文件,其中包含以下字段:字段1,字段2,字段3,频率,我想将其分配给Java中的哈希映射变量。 从下面的代码这里是扫描文件,并计算频率的每一行,但我有一个频率的文件已经所以我只需要读了几行。 所以我取代

    // split the transaction into items

        String[] lineSplited = line.split(" "); 
        String itemString = lineSplited[0];
        Integer count = Integer.valueOf(lineSplited[1]);
        mapSupport.put(itemString, count);

在原始代码中

private void DetermineFrequencyOfSingleItems(String input,
            final Map<String, Integer> mapSupport)
            throws FileNotFoundException, IOException {
        //Create object for reading the input file
        BufferedReader reader = new BufferedReader(new FileReader(input));
        String line;
        // for each line (transaction) until the end of file
        while( ((line = reader.readLine())!= null)){ 
            // if the line is  a comment, is  empty or is a
            // kind of metadata
            if (line.isEmpty() == true ||
                    line.charAt(0) == '#' || line.charAt(0) == '%'
                            || line.charAt(0) == '@') {
                continue;
            }

            // split the transaction into items
            String[] lineSplited = line.split(" ");
             // for each item in the transaction
            for(String itemString : lineSplited){ 
                // increase the support count of the item
                Integer count = mapSupport.get(itemString);
                if(count == null){
                    mapSupport.put(itemString, 1);
                }else{
                    mapSupport.put(itemString, ++count);
                }
            }
            // increase the transaction count
            transactionCount++;
        }
        // close the input file
        reader.close();
    } 

但这不起作用,有什么建议吗?

在原始程序中,对行频进行计数,因此使用“”(空格)分隔CSV行没有区别。

但是,由于您正在读取数据,因此在用作映射中的键或解析为Integer之前,必须使用“,”(逗号)进行拆分并修剪String。

并且请具体说明您的问题以及遇到的错误类型。

由于您的文件以制表符分隔,并且您希望最后一个字段为count,其余字段为键值,请尝试

String frequency = line.substring(line.lastIndexOf('\t')+1);// Parse as Integer 
String key=line.substring(0, line.lastIndexOf('\t'));
mapSupport.put(key,Integer.parseInt(frequency));

暂无
暂无

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

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