• <bdo id='AVvM0'></bdo><ul id='AVvM0'></ul>
    1. <small id='AVvM0'></small><noframes id='AVvM0'>

      <legend id='AVvM0'><style id='AVvM0'><dir id='AVvM0'><q id='AVvM0'></q></dir></style></legend>

      <i id='AVvM0'><tr id='AVvM0'><dt id='AVvM0'><q id='AVvM0'><span id='AVvM0'><b id='AVvM0'><form id='AVvM0'><ins id='AVvM0'></ins><ul id='AVvM0'></ul><sub id='AVvM0'></sub></form><legend id='AVvM0'></legend><bdo id='AVvM0'><pre id='AVvM0'><center id='AVvM0'></center></pre></bdo></b><th id='AVvM0'></th></span></q></dt></tr></i><div id='AVvM0'><tfoot id='AVvM0'></tfoot><dl id='AVvM0'><fieldset id='AVvM0'></fieldset></dl></div>
    2. <tfoot id='AVvM0'></tfoot>

    3. 从文本文件中删除重复行?

      时间:2023-08-27
        <i id='WsltB'><tr id='WsltB'><dt id='WsltB'><q id='WsltB'><span id='WsltB'><b id='WsltB'><form id='WsltB'><ins id='WsltB'></ins><ul id='WsltB'></ul><sub id='WsltB'></sub></form><legend id='WsltB'></legend><bdo id='WsltB'><pre id='WsltB'><center id='WsltB'></center></pre></bdo></b><th id='WsltB'></th></span></q></dt></tr></i><div id='WsltB'><tfoot id='WsltB'></tfoot><dl id='WsltB'><fieldset id='WsltB'></fieldset></dl></div>
          <bdo id='WsltB'></bdo><ul id='WsltB'></ul>

            <small id='WsltB'></small><noframes id='WsltB'>

              <tbody id='WsltB'></tbody>
            1. <tfoot id='WsltB'></tfoot>
            2. <legend id='WsltB'><style id='WsltB'><dir id='WsltB'><q id='WsltB'></q></dir></style></legend>

                本文介绍了从文本文件中删除重复行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                给定一个文本行的输入文件,我希望识别和删除重复的行.请展示一个简单的 C# 片段来完成此操作.

                Given an input file of text lines, I want duplicate lines to be identified and removed. Please show a simple snippet of C# that accomplishes this.

                推荐答案

                应该这样做(并且会复制大文件).

                This should do (and will copy with large files).

                注意它只删除重复的连续行,即

                Note that it only removes duplicate consecutive lines, i.e.

                a
                b
                b
                c
                b
                d
                

                最终会变成

                a
                b
                c
                b
                d
                

                如果您不想在任何地方重复,则需要保留一组您已经看过的行.

                If you want no duplicates anywhere, you'll need to keep a set of lines you've already seen.

                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,并且您要使用文件.不过,它很容易概括为一种方法:

                Note that this assumes Encoding.UTF8, and that you want to use files. It's easy to generalize as a method though:

                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;
                        }
                    }
                }
                

                (请注意,这不会关闭任何东西 - 调用者应该这样做.)

                (Note that that doesn't close anything - the caller should do that.)

                以下版本将删除所有个重复项,而不仅仅是连续的:

                Here's a version that will remove all duplicates, rather than just consecutive ones:

                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);
                        }
                    }
                }
                

                这篇关于从文本文件中删除重复行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:删除列表中的重复对象 (C#) 下一篇:在字典中查找重复值并打印重复元素的键

                相关文章

                最新文章

                <legend id='t5iY8'><style id='t5iY8'><dir id='t5iY8'><q id='t5iY8'></q></dir></style></legend>
              • <i id='t5iY8'><tr id='t5iY8'><dt id='t5iY8'><q id='t5iY8'><span id='t5iY8'><b id='t5iY8'><form id='t5iY8'><ins id='t5iY8'></ins><ul id='t5iY8'></ul><sub id='t5iY8'></sub></form><legend id='t5iY8'></legend><bdo id='t5iY8'><pre id='t5iY8'><center id='t5iY8'></center></pre></bdo></b><th id='t5iY8'></th></span></q></dt></tr></i><div id='t5iY8'><tfoot id='t5iY8'></tfoot><dl id='t5iY8'><fieldset id='t5iY8'></fieldset></dl></div>
                <tfoot id='t5iY8'></tfoot>
                  <bdo id='t5iY8'></bdo><ul id='t5iY8'></ul>

                <small id='t5iY8'></small><noframes id='t5iY8'>