根据标题合并文本文件。

时间:2015-05-24 17:53:25

标签: c# visual-studio-2010 visual-studio visual-studio-2012 split

我正在寻找一些关于如何根据标题合并多个文本文件的建议。

所以说我有以下3个文本文件,标题如下。您可以看到一些元素重复,一些元素是文本文件的唯一元素。文本文件在每个标题下包含数千行,包含各种类型的数据。

文本文件1:

元素1 |元素2 |元素4 |元素5 | 00000001 | 00000002 | 00000004 | 00000005 |

文本文件2:

元素2 |元素3 |元素4 |元素5 | 00000002 | 00000003 | 00000004 | 00000005 |

文本文件3:

元素1 |元素3 |元素4 |元素6 | 00000001 | 00000003 | 00000004 | 00000006 |

最终输出文本文件将如下所示:

元素1 |元素2 |元素4 |元素5 |元素3 |元素6 | 00000001 | 00000002 | 00000004 | 00000005 | 00000003 | ________ | ________ | 00000002 | 00000004 | 00000005 | ________ | ________ | 00000001 | ________ | 00000004 | ________ | 00000003 | 00000006 |

如您所见,输出文本文件将捕获原始3个文本文件中的每个标题。然后它将每个数据字段存储在适当的标题下。如果某个元素在特定的文本文件中不可用,则该字段为空。

1 个答案:

答案 0 :(得分:1)

您应该向我们展示您已经拥有的代码,然后询问如何处理您无法解决的问题。

我建议您存储标题列表,以及行列表或字典。这样您就可以检查标头是否已存在。

例如:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace TestElements
{
    public class Elements
    {
        public static string[] ROW_SEPARATOR = { " " };
        public static string[] ELEMENT_SEPARATOR = { "|" };

        private int _nextRowId;
        public List<string> ColumnHeaders;
        public Dictionary<int, Dictionary<string, string>> Rows;

        public Elements()
        {
            this._nextRowId = 0;
            this.ColumnHeaders = new List<string>();
            this.Rows = new Dictionary<int, Dictionary<string, string>>();
        }

        public void AddFromFile(string path)
        {
            // Read all the file, and split in lines
            string[] lines = File.ReadAllText(path).Split(ROW_SEPARATOR, StringSplitOptions.None);

            // Get the headers
            List<string> headers = lines[0].Split(ELEMENT_SEPARATOR, StringSplitOptions.None).ToList();

            // Add headers that are new
            foreach (string header in headers)
            {
                if (!this.ColumnHeaders.Contains(header))
                {
                    this.ColumnHeaders.Add(header);
                }
            }

            // Parse every line
            for (int i = 1; i < lines.Length; i++)
            {
                // Split a line into elements
                List<string> elements = lines[i].Split(ELEMENT_SEPARATOR, StringSplitOptions.None).ToList();

                // Build a row of elements
                Dictionary<string, string> row = new Dictionary<string,string>();
                for (int j = 0; j < headers.Count; j++)
                {
                    row.Add(headers[j], elements[j]);
                }

                // Add the row to our store
                this.AddRow(row);
            }
        }

        private void AddRow(Dictionary<string, string> rowdata)
        {
            this.Rows.Add(this._nextRowId, rowdata);
            this._nextRowId++;
        }
    }
}

请注意我在这个例子中没有检查错误。

然后您可以根据需要构建输出文本。

如果你有任何更具体的问题,或者这没有用,请问问。

编辑:以下是使用示例

        // Create a few files to use in a test
        string TextFile1 = 
            "Element1|Element2|Element4|Element5| " +
            "00000001|00000002|00000004|00000005| " +
            "00000011|00000012|00000014|00000015| " +
            "00000021|00000022|00000024|00000025| " +
            "00000031|00000032|00000034|00000035|";
        string TextFile2 =
            "Element2|Element3|Element4|Element5| " +
            "00000002|00000003|00000004|00000005| " +
            "00000012|00000013|00000014|00000015| " +
            "00000022|00000023|00000024|00000025|";
        string TextFile3 =
            "Element1|Element3|Element4|Element6| " +
            "00000001|00000003|00000004|00000006| " +
            "00000011|00000013|00000014|00000016| " +
            "00000021|00000023|00000024|00000026| " +
            "00000031|00000033|00000034|00000036| " +
            "00000041|00000042|00000044|00000045|";

        File.WriteAllText("File1.txt", TextFile1);
        File.WriteAllText("File2.txt", TextFile2);
        File.WriteAllText("File3.txt", TextFile3);

        // Read the files into our class
        Elements elements = new Elements();
        elements.AddFromFile("File1.txt");
        elements.AddFromFile("File2.txt");
        elements.AddFromFile("File3.txt");

        // Build the result
        StringBuilder sb = new StringBuilder();

        // First build headers
        foreach (string header in elements.ColumnHeaders)
        {
            sb.Append(header);
            sb.Append("|");
        }
        sb.Append(Environment.NewLine);

        // Next add every row
        foreach (Dictionary<string, string> row in elements.Rows.Values)
        {
            foreach (string header in elements.ColumnHeaders)
            {
                if (row.ContainsKey(header))
                {
                    sb.Append(row[header]);
                }
                else
                {
                    sb.Append("________");
                }
                sb.Append("|");
            }
            sb.Append(Environment.NewLine);
        }

        // Finally save the result into a file
        File.WriteAllText("Result.txt", sb.ToString());

结果如下:

Element1|Element2|Element4|Element5|Element3|Element6|
00000001|00000002|00000004|00000005|________|________|
00000011|00000012|00000014|00000015|________|________|
00000021|00000022|00000024|00000025|________|________|
00000031|00000032|00000034|00000035|________|________|
________|00000002|00000004|00000005|00000003|________|
________|00000012|00000014|00000015|00000013|________|
________|00000022|00000024|00000025|00000023|________|
00000001|________|00000004|________|00000003|00000006|
00000011|________|00000014|________|00000013|00000016|
00000021|________|00000024|________|00000023|00000026|
00000031|________|00000034|________|00000033|00000036|
00000041|________|00000044|________|00000042|00000045|