在java中将两个csv文件合并为一个

发布于 2024-12-21 19:35:46 字数 204 浏览 3 评论 0原文

我有两个 csv 文件,其中包含多个表中的多列。我正在使用 opencsv 来制作 csv 文件。 我想制作一个包含两个文件中所有列的 csv 文件。 这两个文件中有一个公共列。 但记录数并不相同。 请建议一些东西。任何帮助将不胜感激。

PS:连接两个文件只是意味着我想将所有列添加到一个文件中。这不是数据库连接。 我想要合并的 csv 文件并在某些工具中使用它来生成 pdf

I have two csv files with multiple columns from multiple tables.I am using opencsv to make csv files.
I want to make one csv file containing all the columns from both files.
There is one common column in both the files.
But number of records are not same.
Please suggest something. Any help would be appreciated.

P.S.: Joining two files simply mean i want to add all the columns in one file..It is not the database join .
I want the combined csv file and use it to in some tool to generate a pdf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

坦然微笑 2024-12-28 19:35:46

将一个文件加载到由公共列值作为键控的字典中,然后将第二个文件的所有记录附加到字典中的相应条目(同样通过公共列值)。
最后,将所有字典 k,v 对写入新文件。

即兴示例:

CSVReader r1 = ...; // reader of 1st file
CSVReader r2 = ...; // reader of 2nd file

HashMap<String,String[]> dic = new HashMap<String,String[]>();

int commonCol = 1; // index of the commonColumn

r1.readNext(); // skip header
String[] line = null;
while ((line = r1.readNext()) != null)
{
  dic.add(line[commonCol],line)
}

commonCol = 2; // index of the commonColumn in the 2nd file

r2.readNext(); // skip header
String[] line = null;
while ((line = r2.readNext()) != null)
{
  if (dic.keySet().contains(line[commonCol])
  {
    // append line to existing entry
  }
  else
  {
     // create a new entry and pre-pend it with default values
     // for the columns of file1
  }
}

foreach (String[] line : dic.valueSet())
{
   // write line to the output file.
}

Load one file into a dictionary keyed by the common column value, then append all the records of the 2nd file to the respective entry in the dictionary (again by common column value).
Finally, write all dictionary k,v pairs to a new file.

improvised example:

CSVReader r1 = ...; // reader of 1st file
CSVReader r2 = ...; // reader of 2nd file

HashMap<String,String[]> dic = new HashMap<String,String[]>();

int commonCol = 1; // index of the commonColumn

r1.readNext(); // skip header
String[] line = null;
while ((line = r1.readNext()) != null)
{
  dic.add(line[commonCol],line)
}

commonCol = 2; // index of the commonColumn in the 2nd file

r2.readNext(); // skip header
String[] line = null;
while ((line = r2.readNext()) != null)
{
  if (dic.keySet().contains(line[commonCol])
  {
    // append line to existing entry
  }
  else
  {
     // create a new entry and pre-pend it with default values
     // for the columns of file1
  }
}

foreach (String[] line : dic.valueSet())
{
   // write line to the output file.
}
顾冷 2024-12-28 19:35:46

如果我们知道哪些列有重复数据,我们可以这样做

    int n1,n2;//stores the serial number of the column that has the duplicate data
    BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream(f1)));
    BufferedReader br2=new BufferedReader(new InputStreamReader(new FileInputStream(f2)));
    String line1,line2;
    while((line1=br1.readLine())!=null && (line2=br2.readLine())!=null){
        String line=line1+","+line2;
        String newL="";
        StringTokenizer st=new StringTokenizer(line,",");
        for(int i=1;i<=st.countTokens();i++){
            if((i==n1)||(i==n1+n2))
                continue;
            else
                newL=newL+","+st.nextToken();
        }
        String l=newL.substring(1);
        //write this line to the output file
    }

We can do something like this if we know which columns have the duplicate data

    int n1,n2;//stores the serial number of the column that has the duplicate data
    BufferedReader br1=new BufferedReader(new InputStreamReader(new FileInputStream(f1)));
    BufferedReader br2=new BufferedReader(new InputStreamReader(new FileInputStream(f2)));
    String line1,line2;
    while((line1=br1.readLine())!=null && (line2=br2.readLine())!=null){
        String line=line1+","+line2;
        String newL="";
        StringTokenizer st=new StringTokenizer(line,",");
        for(int i=1;i<=st.countTokens();i++){
            if((i==n1)||(i==n1+n2))
                continue;
            else
                newL=newL+","+st.nextToken();
        }
        String l=newL.substring(1);
        //write this line to the output file
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文