[英]reading CSV file into hashmap
I have a CSV file which contain fields as follows: Field 1, Field 2,Field 3, the frequency , and I want to assign it to hash map variable in Java. 我有一个CSV文件,其中包含以下字段:字段1,字段2,字段3,频率,我想将其分配给Java中的哈希映射变量。 the code below from here is to scan the file and calculate the frequency for each line, However I have the file with frequencies already so I just need to read a few lines.
从下面的代码这里是扫描文件,并计算频率的每一行,但我有一个频率的文件已经所以我只需要读了几行。 so I replace
所以我取代
// split the transaction into items
with 与
String[] lineSplited = line.split(" ");
String itemString = lineSplited[0];
Integer count = Integer.valueOf(lineSplited[1]);
mapSupport.put(itemString, count);
in the original code 在原始代码中
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();
}
but it does not work, any suggestion? 但这不起作用,有什么建议吗?
In the original program, the line frequency is counted so the CSV lines are split using " "(space) makes no difference. 在原始程序中,对行频进行计数,因此使用“”(空格)分隔CSV行没有区别。
But since you are reading the data, you have to split using "," (comma) and trim the String before using as key in your map or parsing as Integer. 但是,由于您正在读取数据,因此在用作映射中的键或解析为Integer之前,必须使用“,”(逗号)进行拆分并修剪String。
And please be specific about your problem and what kind of error you get. 并且请具体说明您的问题以及遇到的错误类型。
Since your file is tab spaced and you want the last field as count and rest as the key value, try 由于您的文件以制表符分隔,并且您希望最后一个字段为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.