gpt4 book ai didi

java - 在 csv 文件中搜索列单词并将其替换为另一个值 java

转载 作者:行者123 更新时间:2023-12-01 12:55:11 25 4
gpt4 key购买 nike

我有两个 csv 文件,主文件和外观文件。主文件包含这样的单词列表:

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的第二行?

最佳答案

在发布我的解决方案之前需要注意一些事项:

  • 在读取文件时没有简单的方法来替换文件的内容,您必须创建一个新文件并在那里输出您需要的内容
  • 如果你想替换值,那么这是使用 map 的好地方
  • 您的代码意味着 look.csv 文件仅包含 2 行

以下内容将实现您正在寻找的内容,但同样,输出位于不同的文件中(如果您愿意,可以在之后重命名):

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;
}

}

关于java - 在 csv 文件中搜索列单词并将其替换为另一个值 java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23969007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com