将 CSV 文件读入 hashmap
reading CSV file into hashmap
我有一个 CSV 文件,其中包含如下字段:
字段 1、字段 2、字段 3、频率,我想将它分配给 Java 中的哈希映射变量。 here 下面的代码是扫描文件并计算每一行的频率,但是我已经有了包含频率的文件,所以我只需要阅读几行。所以我替换
// 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行用“”分割(space)没有区别。
但是由于您正在读取数据,因此在将字符串用作地图中的键或解析为整数之前,必须使用“,”(逗号)和 trim 拆分字符串。
请具体说明您遇到的问题以及遇到的错误类型。
由于您的文件是制表符 spaced,并且您希望最后一个字段作为计数,其余字段作为键值,请尝试
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));
我有一个 CSV 文件,其中包含如下字段: 字段 1、字段 2、字段 3、频率,我想将它分配给 Java 中的哈希映射变量。 here 下面的代码是扫描文件并计算每一行的频率,但是我已经有了包含频率的文件,所以我只需要阅读几行。所以我替换
// 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行用“”分割(space)没有区别。
但是由于您正在读取数据,因此在将字符串用作地图中的键或解析为整数之前,必须使用“,”(逗号)和 trim 拆分字符串。
请具体说明您遇到的问题以及遇到的错误类型。
由于您的文件是制表符 spaced,并且您希望最后一个字段作为计数,其余字段作为键值,请尝试
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));