给定一个文本行的输入文件,我想重复的行被识别和删除。请展示一个简单的C#代码片段来完成此操作。删除文本文件中的重复行?
回答
这应该做(和将复制大文件)。
注意,它只是删除了重复连续线,即
a
b
b
c
b
d
最终将成为
a
b
c
b
d
如果你想在任何地方没有重复,你需要保留一组你已经看到的线条。
using System;
using System.IO;
class DeDuper
{
static void Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("Usage: DeDuper <input file> <output file>");
return;
}
using (TextReader reader = File.OpenText(args[0]))
using (TextWriter writer = File.CreateText(args[1]))
{
string currentLine;
string lastLine = null;
while ((currentLine = reader.ReadLine()) != null)
{
if (currentLine != lastLine)
{
writer.WriteLine(currentLine);
lastLine = currentLine;
}
}
}
}
}
请注意,这里假定Encoding.UTF8
,并且您要使用文件。这很容易概括,就像一个方法:
static void CopyLinesRemovingConsecutiveDupes
(TextReader reader, TextWriter writer)
{
string currentLine;
string lastLine = null;
while ((currentLine = reader.ReadLine()) != null)
{
if (currentLine != lastLine)
{
writer.WriteLine(currentLine);
lastLine = currentLine;
}
}
}
(请注意,不会关闭任何 - 调用方应该做的)
这里有一个版本,将删除所有重复,而不是只是连续的:
static void CopyLinesRemovingAllDupes(TextReader reader, TextWriter writer)
{
string currentLine;
HashSet<string> previousLines = new HashSet<string>();
while ((currentLine = reader.ReadLine()) != null)
{
// Add returns true if it was actually added,
// false if it was already there
if (previousLines.Add(currentLine))
{
writer.WriteLine(currentLine);
}
}
}
对于长文件(和非连续的重复),我会用线建设哈希//位置查找表我去的文件拷贝一行。
复制每一行检查散列值,如果存在冲突,请检查该行是否相同并移至下一行。 (
只有值得的,虽然相当大的文件。
这里有一个流媒体的方式应该比读取所有唯一字符串到内存中招致更少的开销。
var sr = new StreamReader(File.OpenRead(@"C:\Temp\in.txt"));
var sw = new StreamWriter(File.OpenWrite(@"C:\Temp\out.txt"));
var lines = new HashSet<int>();
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
int hc = line.GetHashCode();
if(lines.Contains(hc))
continue;
lines.Add(hc);
sw.WriteLine(line);
}
sw.Flush();
sw.Close();
sr.Close();
它需要较少的内存,但如果发生散列冲突,它也会产生不正确的输出。 – 2009-08-07 20:18:54
我是新来的.NET &有写一些更简单的东西,可能效率不高。请免费分享您的想法。
class Program
{
static void Main(string[] args)
{
string[] emp_names = File.ReadAllLines("D:\\Employee Names.txt");
List<string> newemp1 = new List<string>();
for (int i = 0; i < emp_names.Length; i++)
{
newemp1.Add(emp_names[i]); //passing data to newemp1 from emp_names
}
for (int i = 0; i < emp_names.Length; i++)
{
List<string> temp = new List<string>();
int duplicate_count = 0;
for (int j = newemp1.Count - 1; j >= 0; j--)
{
if (emp_names[i] != newemp1[j]) //checking for duplicate records
temp.Add(newemp1[j]);
else
{
duplicate_count++;
if (duplicate_count == 1)
temp.Add(emp_names[i]);
}
}
newemp1 = temp;
}
string[] newemp = newemp1.ToArray(); //assigning into a string array
Array.Sort(newemp);
File.WriteAllLines("D:\\Employee Names.txt", newemp); //now writing the data to a text file
Console.ReadLine();
}
}
有一个想法:如果你可以评论你的代码来解释你在做什么(以及为什么),这会很有用 - 这将有助于他人理解你的方法并将其应用到他们未来的情况。 – 2016-04-14 19:59:02
- 1. 删除CSV文件中的重复行
- 2. 删除在文本文件中的非重复的行
- 3. 批处理从文本文件中删除重复的行
- 4. 如何从文本文件中删除重复的行
- 5. 删除重复文本
- 6. 删除文本行和重写文件
- 7. 删除列中的重复文本
- 8. 删除Textarea中的重复文本
- 9. 对文本文件进行排序并删除重复项
- 10. 如何删除文本文件中的重复链接?
- 11. 删除大文本文件中的重复
- 12. 如何从文件中删除重复的文本?
- 13. 从文本文件中删除重复和周围的线
- 14. 删除文件中的重复项
- 15. 删除Unix中的重复文件
- 16. 从C#中的文本文件中删除带有时间戳的重复行
- 17. 删除文本文件中的行
- 18. 删除文本文件中的行
- 19. 从文本文件中删除重复项
- 20. 从文本文件中删除重复单词
- 21. 批处理文件脚本删除文件夹中重复的文件
- 22. 如何从文本文件中删除具有重复值的行
- 23. 从文本文件中删除一行?
- 24. 从文本文件中删除行
- 25. 从文本文件中删除多行
- 26. 在文本文件中删除单行?
- 27. 从复制文本中删除文本
- 28. 删除文本文件中的文本行的上下文
- 29. 在Java中,从文件列表中删除重复的文件
- 30. 使用Java从文本中删除重复的行
有很多方法,有些更容易实现相对于其它的。要采取的方法可能取决于文本文件的大小和匹配行的预期数量。你能描述你想解决的具体问题吗?谢谢:) – 2009-08-07 15:47:21
。 。 。和期望的表现。 – 2009-08-07 15:48:29