提取 CSV 值为键值对。

huangapple go评论80阅读模式
英文:

Extract csv values into key value pairs

问题

我有一个带有两列的CSV文件;ColumnA和ColumnB。我想提取ColumnA和ColumnB的两个值,以便稍后在for循环中解析它们。

CSV示例:

John, John Smith
James, James Bond

当我对变量oldName进行系统打印时,我的程序成功地提取了ColumnA的值。然而,我不知道如何提取ColumnB的值,我认为原因是我选择的数据结构,我不知道如何正确地调整它以满足我的需求?

基本上,我希望在我的for循环中,变量oldName = "John",变量newName = "John Smith",用于第一次迭代,以及oldName = "James",变量newName = "James Bond",用于第二次迭代等。

String fName = "TestFile.csv";
String thisLine;
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
String oldName;
String newName;
int i=0;

String[] GroupArray = new String[1000];
while ((thisLine = myInput.readLine()) != null)
{
    String strar[] = thisLine.split(",");
    
    for(int j=0;j<strar.length;j++)
    {
        if(i!=0)
        {
            GroupArray[i] = strar[j];
        }
        else
        {
            GroupArray[i] = strar[j];
        }
    }
    i++;
}
try{
    for (int l = 0; l < i; l++)
    {
        oldName = GroupArray[0];
        newName = GroupArray[1];
        out.println ("User: "+ oldName +" has been renamed to: "+ newName +"<BR>");
    }

以上为翻译内容,不包含代码部分。

英文:

I have a CSV file with two columns; ColumnA and ColumnB. I want to extract both values of ColumnA and ColumnB to parse them later in a for loop.

Example of CSV:

John, John Smith
James, James Bond

My program extracts successfully ColumnA when I make a system print of the variable oldName. However, I do not know how to extact the value of ColumnB, and I think the reason is the data structure I have chosen, which I do not know how to correctly adapt for my needs?

Basically, I want for my for loop that the variable oldName = "John" and newName = "John Smith" for the first iteration, and for the second oldName = "James" and newName = "James Bond" etc.

String fName = &quot;TestFile.csv&quot;;
String thisLine; 
FileInputStream fis = new FileInputStream(fName);
DataInputStream myInput = new DataInputStream(fis);
String oldName;
String newName;
int i=0; 

String[] GroupArray = new String[1000]; 
while ((thisLine = myInput.readLine()) != null)
{
	String strar[] = thisLine.split(&quot;,&quot;);
	
for(int j=0;j&lt;strar.length;j++)
{
if(i!=0)
{
GroupArray[i] = strar[j];
}
else
{
GroupArray[i] = strar[j];
}
} 
i++;
} 
   try{	
	for (int l = 0; l &lt; i; l++)
	{
		oldName = GroupArray[0];
		newName = GroupArray[1];
        out.println (&quot;User: &quot;+ oldName +&quot; has been renamed to: &quot;+ newName +&quot;&lt;BR&gt;&quot;);

答案1

得分: 1

你的代码本质上是正确的,但有一些多余的代码。需要注意的是,DataInputStream#readLine 方法已被弃用,因此我使用了 BufferedReader#readLine 方法。

String filename = "/path/to/your/file";
File csv = new File(filename);
BufferedReader reader = new BufferedReader(new FileReader(csv));

String[] groupArray = new String[1000];

String line;
int i = 0;
while ((line = reader.readLine()) != null) {
    String[] split = line.split(",");

    groupArray[i] = split[0].trim();
    groupArray[i + 1] = (split.length > 1) ? split[1].trim() : "";

    i += 2;
}

String oldName, newName;
for (int j = 0; j < i; j += 2) {
    oldName = groupArray[j];
    newName = groupArray[j + 1];
    System.out.println("User: " + oldName + " has been renamed to: " + newName + "<BR>");
}

输入:

John, John Smith
James, James Bond

输出:

User: John has been renamed to: John Smith<BR>
User: James has been renamed to: James Bond<BR>
英文:

You were on the right track but you had some superfluous code. Note that DataInputStream#readLine is deprecated, thus I used BufferedReader#readLine.

String filename = &quot;/path/to/your/file&quot;;
File csv = new File(filename);
BufferedReader reader = new BufferedReader(new FileReader(csv));

String[] groupArray = new String[1000];

String line;
int i = 0;
while((line = reader.readLine()) != null) {
	String[] split = line.split(&quot;,&quot;);
	
	groupArray[i] = split[0].trim();
	groupArray[i+1] = (split.length &gt; 1) ? split[1].trim() : &quot;&quot;;
	
	i += 2;
}

String oldName, newName;
for(int j = 0; j &lt; i; j += 2) {
	oldName = groupArray[j];
	newName = groupArray[j+1];
	System.out.println (&quot;User: &quot;+ oldName +&quot; has been renamed to: &quot;+ newName +&quot;&lt;BR&gt;&quot;);
}

Input:

John, John Smith
James, James Bond

Ouput:

User: John has been renamed to: John Smith&lt;BR&gt;
User: James has been renamed to: James Bond&lt;BR&gt;

答案2

得分: 1

我认为你在不必要地使任务变得过于复杂。以下示例应该作为一个起点:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) throws IOException {
        File file = new File("TestFile.csv");
        BufferedReader br = new BufferedReader(new FileReader(file));

        String line;
        List<String[]> allLines = new ArrayList<>();
        while ((line = br.readLine()) != null) {
            String[] splited = line.split("\\s*,\\s*");
            allLines.add(splited);                
        }
        
        for(String[] row : allLines){
            String oldName = row[0];
            String newName = row[1];
            System.out.println("User: "+ oldName +" has been renamed to: "+ newName );
        }
    }
}
英文:

I think you are overcomplicating the task without need. The following example should serve as a starting point:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Test {

    public static void main(String[] args) throws IOException {
        File file = new File(&quot;TestFile.csv&quot;);
        BufferedReader br = new BufferedReader(new FileReader(file));

        String line;
        List&lt;String[]&gt; allLines = new ArrayList&lt;&gt;();
        while ((line = br.readLine()) != null) {
            String[] splited = line.split(&quot;\\s*,\\s*&quot;);
            allLines.add(splited);                
        }
        
        for(String[] row : allLines){
            String oldName = row[0];
            String newName = row[1];
            System.out.println (&quot;User: &quot;+ oldName +&quot; has been renamed to: &quot;+ newName );
        }
    }
}

huangapple
  • 本文由 发表于 2020年10月8日 07:24:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/64253647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定