我有三个文本文件:file1 file2和file3,所有文件都包含电子邮件。 file1应该有所有的电子邮件,file2有所有的电子邮件是A-M,文件3有来自n-z的电子邮件(这不重要,但我认为这有助于提供一些上下文。)
我正在c#中编写一个控制台应用程序,它将查看这三个文件,如果有一个不是1的电子邮件应该是,那么它将写入一个主文件,它将说明需要什么添加到什么。
例如,假设我有电子邮件john@example.com
。如果在file1中找到但在file2中找不到,则主文件的输出必须为"this email needs to be added to file2: john@example.com"
。现在如果它被反转,并且在file2中找到了电子邮件但在file1中找不到,那么输出应为"this email needs to be added to file1: john@example.com"
作为我的代码的一部分,我正在寻找的答案需要在某种foreach循环和if语句中,但是我有点迷失在我需要放入的内容中。如果有人可以帮我解决问题在我的陈述中我必须使用它,我将非常感激。如果有人对此有任何疑问,请随时提问!
//Making a list for file1
List<string> listFullPack = new List<string>();
string line;
StreamReader sr = new StreamReader("file1");
while ((line = sr.ReadLine()) != null)
{
listFile1.Add(line);
}
sr.Close();
//Making a list for file2
List<string> listDen1 = new List<string>();
string line1;
StreamReader sr1 = new StreamReader("file2");
while ((line1 = sr1.ReadLine()) != null)
{
listFile2.Add(line1);
}
sr1.Close();
//Making a list for file3
List<string> listDen2 = new List<string>();
string line2;
StreamReader sr2 = new StreamReader("file3");
while ((line2 = sr2.ReadLine()) != null)
{
listFile3.Add(line2);
}
sr2.Close();
//This will double check that emails are in
foreach (string element in listFullPack)
{
System.Console.WriteLine(element);
Debug.WriteLine(element);
if (element == "jimbob@example.com")
{
Debugger.Break();
}
}
//this will compare the file1 list to the file2 list
var firstNotSecond = listFile1.Except(listFile2).ToList();
var secondNotFirst = listFile2.Except(listFile1).ToList();
//this will compare the file2 list to the file3 list
var firstNotThird = listFile1.Except(listFile3).ToList();
var thirdNotFirst = listFile3.Except(listFile1).ToList();
//this will compare the file2 list to the file3 list
var secondNotThird = listFile2.Except(listFile3).ToList();
var thirdNotSecond = listFile3.Except(listFile2).ToList();
foreach (string element in listFile1) // This is where I am lost
{
if (!)
{
}
}
答案 0 :(得分:1)
您可以尝试这样简单的事情:
//Making a list for file1
HashSet<string> listFile1 = new HashSet<string>();
string line;
StreamReader sr = new StreamReader("file1");
while ((line = sr.ReadLine()) != null)
{
listFile1.Add(line);
}
sr.Close();
//Making a list for file2
HashSet<string> listFile2 = new HashSet<string>();
string line1;
StreamReader sr1 = new StreamReader("file2");
while ((line1 = sr1.ReadLine()) != null)
{
listFile2.Add(line1);
}
sr1.Close();
//Making a list for file3
HashSet<string> listFile3 = new HashSet<string>();
string line2;
StreamReader sr2 = new StreamReader("file3");
while ((line2 = sr2.ReadLine()) != null)
{
listFile3.Add(line2);
}
sr2.Close();
IEnumerable<string> allEmails = listFile1.Union(listFile2).Union(listFile3);
// this will double check the emails
foreach (string element in allEmails)
{
if (!listFile1.Contains(element))
System.Console.WriteLine("file 1 is missing " + element);
int firstCharAscii = element.Trim().ToLower()[0];
if (firstCharAscii < 110)
{
// less than "n"
if (!listFile2.Contains(element))
System.Console.WriteLine("file 2 is missing " + element);
if (listFile3.Contains(element))
System.Console.WriteLine("file 3 erroneously contains " + element);
}
else
{
// "n" or greater
if (!listFile3.Contains(element))
System.Console.WriteLine("file 3 is missing " + element);
if (listFile2.Contains(element))
System.Console.WriteLine("file 2 erroneously contains " + element);
}
}
请记住,随着电子邮件数量的增长,List<string>.Contains()
方法成为确定是否存在的低效方式。你会更适合HashSet<string>
课程。此外,如果出于任何原因您使用Unicode字符串进行读取,则需要一种更健壮的方法来检查第一个字符的值。
答案 1 :(得分:0)
List<string> fullList = File.ReadAllLines("fullist.txt").ToList<string>();
List<string> firstList = File.ReadAllLines("list1.txt").ToList<string>();
List<string> secondList = File.ReadAllLines("list2.txt").ToList<string>();
firstList.ForEach(m => {if (!fullList.Contains(m)/*Or other logic*/) {fullList.Add(m+" from 1 Needs to be in master");}});
secondList.ForEach(m => {if (!fullList.Contains(m)/*Or other logic*/) {fullList.Add(m+ " from 2 Needs to be in master");}});
答案 2 :(得分:0)
这应该为你做。
List<string> file1Parsed = new List<string>();
List<string> file2Parsed = new List<string>();
List<string> file3Parsed = new List<string>();
using (StreamReader readerFile1 = new StreamReader(@"c:\file1.txt"))
{
while (!readerFile1.EndOfStream)
{
file1Parsed.Add(readerFile1.ReadLine());
}
}
using (StreamReader readerFile2 = new StreamReader(@"c:\file2.txt"))
{
while (!readerFile2.EndOfStream)
{
file2Parsed.Add(readerFile2.ReadLine());
}
}
using (StreamReader readerFile3 = new StreamReader(@"c:\file3.txt"))
{
while (!readerFile3.EndOfStream)
{
file3Parsed.Add(readerFile3.ReadLine());
}
}
char[] firstSet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M' };
char[] secondSet = { 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
var file1InFile2 = file1Parsed.Where(y => y.ToUpper().IndexOfAny(firstSet) == 0).Select(x => x);
var file1InFile3 = file1Parsed.Where(y => y.ToUpper().IndexOfAny(secondSet) == 0).Select(x => x);
using (StreamWriter writer = new StreamWriter(@"C:\notExists.txt"))
{
file1InFile2.Where(x => !file2Parsed.Contains(x.Trim())).ToList().ForEach(y => writer.WriteLine("This email needs to be added to file2: " + y));
file1InFile3.Where(x => !file3Parsed.Contains(x.Trim())).ToList().ForEach(y => writer.WriteLine("This email needs to be added to file3: " + y));
file2Parsed.Where(x => !file1InFile2.Contains(x.Trim())).ToList().ForEach(y => writer.WriteLine("This email needs to be added to file1: " + y));
file3Parsed.Where(x => !file1InFile3.Contains(x.Trim())).ToList().ForEach(y => writer.WriteLine("This email needs to be added to file1: " + y));
}
我修改了它以减少代码