[英]Comparing strings of text files
我有三個文本文件:file1 file2和file3,所有這些文件都包含電子郵件。 文件1應該包含所有電子郵件,文件2具有所有AM電子郵件,文件3具有來自nz的電子郵件(這並不重要,但我認為這會提供一些上下文。)
我正在用c#編寫一個控制台應用程序,該程序將查看這三個文件,並且如果一封電子郵件的位置不為1,則它將寫入主文件,該文件將說明需要添加到什么內容中。 。
例如,假設我有電子郵件john@example.com
。 如果在file1中找到了該文件,而在file2中找不到,則該主文件的輸出必須是"this email needs to be added to file2: john@example.com"
。 現在,如果"this email needs to be added to file1: john@example.com"
其反轉,並且在文件2中找到了該電子郵件,但在文件1中卻找不到,則輸出應為"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 (!)
{
}
}
您可以嘗試類似以下的簡單操作:
//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字符串,則將需要一種更可靠的方法來檢查第一個字符的值。
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");}});
這應該為您做。
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));
}
我用更少的代碼修改了它
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.