问题描述
我想基于一个公共列加入两个csv文件。我的两个csv文件和最终的csv文件如下所示。
sno,first name,last name
--------------------------
1,xx,yy
2,aa,bb
第二个文件如下:
sno,place
-----------
1,pp
2,qq
输出:
sno,last name,place
------------------------------
1,yy,bb,qq
代码:
CSVReader r1 = new CSVReader(new FileReader("c:/csv/file1.csv"));;
CSVReader r2 = new CSVReader(new FileReader("c:/csv/file2.csv"));;
HashMap<String,String[]> dic = new HashMap<String,String[]>();
int commonCol = 1;
r1.readNext(); // skip header
String[] line = null;
while ((line = r1.readNext()) != null)
{
dic.put(line[commonCol],line)
}
commonCol = 1;
r2.readNext();
String[] line2 = null;
while ((line2 = r2.readNext()) != null)
{
if (dic.keySet().contains(line2[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.
}
我不知道如何进一步进行以获得所需的输出。任何帮助将不胜感激。 谢谢
解决方法
首先,您需要使用零作为您的C:\Program Files (x86)\Microsoft Visual Studio\Installer\resources\app\layout\InstallCleanup.exe
值,因为第一列的索引为零而不是一个。
commonCol