从文本文件中读取并使用StreamWriter将其写入另一个文本文件,使其成为逗号分隔格式

dol*_*m77 1 c#

我正在尝试编写已从文本文件中读取的数据,并将其写入另一个文本文件,以逗号分隔的格式.我需要知道代码是什么来得出这个结论.这是我需要帮助的地方.

例:

原始数据如下所示:

Agnico-Eagle Mines
COM
008474108
28996843
716800
716800
N/A
N/A

N/A
716800
N/A
Agrium Inc.
COM
008916108
145739616
1646617
1646617
N/A
N/A

N/A
1646617
N/A
AuRico Gold Inc
COM
05155C105
504505
62875
62875
N/A
N/A

N/A
62875
N/A.

这就是我希望数据在RichTextBox中的样子:

Agnico-Eagle Mines,COM,008474108,28996843,716800,716800,N/A,N/A,N/A,716800,N/A
Agrium Inc.,COM,008916108,145739616,1646617,1646617,N/A ,N/A ,, N/A,1646617,N/A
AuRico Gold Inc,COM,05155C105,504505,62875,62875,N/A,N/A,N/A,62875,N/A

只是你从原始文本数据中知道,我想读取第一行,然后添加一个逗号然后读取第二行将其附加到第一行然后添加一个逗号,这对于前12行是一个.第12行的结尾没有逗号.然后该过程重新开始.

任何信息表示赞赏.

谢谢.

下面是我到目前为止编写的代码.

    private void button1_Click(object sender, EventArgs e)
    {
        using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
        {
            while (!Reader.EndOfStream)
            {
                TextBox1.AppendText(Reader.ReadLine());
            }

        }           

    }

    private void button2_Click(object sender, EventArgs e)
    {
        using (StreamWriter Writer = new StreamWriter(@"C:\Original_Text_File.txt"))
        {

            Writer.WriteLine(TextBox1.Text);

        }
    }
Run Code Online (Sandbox Code Playgroud)

com*_*ech 5

这是我阅读数据的方式:

        var sbText = new System.Text.StringBuilder(10000);
        // Keeps track of your current position within a record
        int wCurrLine = 0;
        // Number of rows in the file that constitute a record
        const int LINES_PER_ROW = 12;

        using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
        {
            while (!Reader.EndOfStream)
            {
                // If we are not on the first row in the record, add a comma
                if (wCurrLine != 0)
                {
                    sbText.Append(",");
                }
                // Add the text
                sbText.Append(Reader.ReadLine());

                // Increment our current record row counter
                wCurrLine++;
                // If we have read all of the rows for this record
                if (wCurrLine == LINES_PER_ROW)
                {
                    // Add a line to our buffer
                    sbText.AppendLine();
                    // And reset our record row count
                    wCurrLine = 0;
                }
            }
            // When all of the data has been loaded, write it to the text box in one fell swoop
            TextBox1.Text = sbText.ToString();
Run Code Online (Sandbox Code Playgroud)

编辑:我刚刚意识到我没有完全回答原始问题:没有理由使用文本框,除非你想在写出结果之前看到结果.如果您不需要这样做,您可以替换该行:

            TextBox1.Text = sbText.ToString();
Run Code Online (Sandbox Code Playgroud)

有:

        using (StreamWriter Writer = new StreamWriter(@"C:\Original_Text_File.csv"))
        {
            Writer.Write(sbText);
        }
Run Code Online (Sandbox Code Playgroud)

(注意文件名中扩展名的更改).


I4V*_*I4V 5

假设12是输入文本的幻数,

var query = File.ReadLines("a.txt")
                .Select((line,no) => new{line,no})
                .GroupBy(x => x.no/12)
                .Select(g => String.Join(",",g.Select(x => x.line)));

File.WriteAllLines("b.txt",query);
Run Code Online (Sandbox Code Playgroud)

这适用于您的样本输入和预期输出....