我正在靠墙撞我的头,我希望有人能指引我朝着正确的方向前进。这不能像我做的那样复杂。读取CSV文件,将第3列与第1列分别匹配在一起
我正在研究一个程序读取一个文件(大约50行左右)的项目,并且我需要让它将第三列上的数据与单独行的第一列上的数据进行匹配。我打开了一个新项目,因为这样一个简单的任务太复杂了。
下面是一个例子文件,该文件是密切相关的实际文件我的工作: a1,b1,c1,d1 **c4**,b2,c2,d2 a3,b3,c3,d3 a4,b4,**c4**,d4 a5,b5,c4,d5
我保证这不是一个学校项目,这是我需要弄清楚工作目的的东西。
这是我的,我知道它不会工作,因为它只是逐行读取比较。如何让程序读取foreach命令中的当前数组值,并对照我在streamreader中捕获的整个文件?
static void Main(string[] args)
{
StreamReader sr = new StreamReader("directories.txt");
string sline = sr.ReadLine();
string[] sarray = sline.Split(',');
string col3 = sarray[2];
string col1 = sarray[0];
foreach(string a in sarray)
{
// ?!?!?!!!
// I know this won't work because I'm comparing the same line being read.
// How in the world can I make this program read col3 of the current line being read against the entire file that was read earlier?
if (col3 == col1)
{
Directory.CreateDirectory("DRIVE:\\Location\\" + a.ToString());
}
}
}
谢谢你提前!
使用'File.ReadAllLines'和工作与整个集合。这不像你有50行的大文件。 – Jonesopolis
评论部分并没有透露与数据匹配需要做什么,但你可以使用linq(GroupBy)找到匹配 – Plutonix