我有两个csv文件,main和look。 主文件包含这样的单词列表:
a,c,e,f
b,d,o,f
和look.csv包含两行,如下所示:
a,f,j
1,0,1
我想搜索main.csv的每个单词,并在look.csv中找到匹配项。如果有匹配,我在main.csv中用look.csv中的第2行中的值替换
我写这段代码
public class lookup {
public static void main(String args[]) throws FileNotFoundException
{
String word;
Scanner main = new Scanner(new File("C:\\Users\\sa\\Desktop\\hotel2\\all.csv"));
main.useDelimiter(",");
Scanner look=new Scanner (new File("C:\\Users\\sa\\Desktop\\comtedad.csv"));
while (main.hasNext())
{
word=main.next() ;
while (look.hasNext()){
if (main.next().equals(look.next())) {//code for replacing}
}
}
main.close();
}}
当两个条目匹配时,如何访问第二行look.scv?
答案 0 :(得分:1)
在发布我的解决方案之前需要注意的一些事项:
以下内容可以实现您的目标,但同样,输出位于不同的文件中(如果您愿意,可以在以后重命名):
public class Main {
public static void main(String[] args) throws Exception {
//1. create Scanners that will look through the CSV files
Scanner main = new Scanner(new File("main.csv"));
Scanner look = new Scanner(new File("look.csv"));
//2. create the final CSV file that will contain both the replaced words and non-replaced words from main.csv
FileWriter replaced = new FileWriter(createReplacedCsv());
//3. store contents of look.csv in a Map
Map<String, String> lookCsvWords = storeLookCsvInSet(look);
//4. Store the contents of the file in a StringBuilder, so you can use the StringBuilder.deleteCharAt(int)
// and .lastIndexOf(String) to delete the last comma
StringBuilder builder = new StringBuilder();
//5. keep track of the line we are on
int line = 0;
//6. iterate through main.csv and search for replaceable words, and write them to replaced.csv if they are found
while (main.hasNext()) {
//search the first line for replaceable chars
String[] wordsInLine = main.nextLine().split(",");
for (String word : wordsInLine) {
if (lookCsvWords.containsKey(word) && line == 0) {
word = lookCsvWords.get(word);
}
builder.append(word).append(",");
}
line ++;
builder.deleteCharAt(builder.lastIndexOf(","));
builder.append("\n");
}
//7. write the contents of the StringBuilder to the file
replaced.write(builder.toString());
replaced.close();
}
/*
* Creates the replaced.csv file if it doesn't exist
*/
private static File createReplacedCsv() throws Exception {
File replacedCsv = new File("replaced.csv");
replacedCsv.createNewFile();
return replacedCsv;
}
/*
* Store the words within look.csv in a Map.
* This method assumes that look.csv has only two lines
*/
private static Map<String, String> storeLookCsvInSet(Scanner lookScanner) throws Exception {
Map<String, String> lookCsvWordMappings = new HashMap<String, String>();
String[] line1Values = lookScanner.nextLine().split(",");
String[] line2Values = lookScanner.nextLine().split(",");
for (int i=0; i<line1Values.length; i++) {
lookCsvWordMappings.put(line1Values[i], line2Values[i]);
}
return lookCsvWordMappings;
}
}
答案 1 :(得分:0)
首先尝试在Map<String,String>
之类的映射数据结构中加载look.csv,如下所示:
Map<String,String> mapping=new HashMap<String,String>()
Scanner look=new Scanner (new File("C:\\Users\\sa\\Desktop\\look.csv"));
String[] keys=look.nextLine().split(",");
String[] values=look.nextLine().split(",");
for(int i=0;i<keys.length;++i)
mapping.put(keys[i],values[i]);
然后在读取主文件的同时写一个新文件;
Scanner main = new Scanner(new File("C:\\Users\\sa\\Desktop\\hotel2\\all.csv"));
PrintWriter mainOutput = new PrintWriter (new File("C:\\Users\\sa\\Desktop\\hotel2\\all.out.csv"));
while (main.hasNext()){
String[] nextTokens=main.nextLine().split(",");
for(String token:nextTokens)
if(mapping.get(token)!=null)
{
mainOutput .print(mapping.get(token)+",");
}else {
mainOutput .print(token+",");
}
mainOutput .println();
}
mainOutput .close();