1

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

例子:

原始数据如下所示:

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 不适用 不适用

适用

62875

适用

这就是我希望数据在 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);

        }
    }
4

6 回答 6

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

这适用于您的示例输入和预期输出....

于 2012-12-28T22:44:32.303 回答
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();

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

            TextBox1.Text = sbText.ToString();

和:

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

(注意文件名中扩展名的变化)。

于 2012-12-28T22:35:22.763 回答
1

如果您只想使用不同的方法从一个文件写入另一个文件,请尝试以下操作:

protected string TextProperty { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    using (StreamReader reader = new StreamReader(@"C:\Original_Text_File.txt"))
    {
        // read all content file to the string property on your form.
        TextProperty = reader.ReadToEnd();    
    }         

}

private void button2_Click(object sender, EventArgs e)
{
    using (StreamWriter writer = new StreamWriter(@"C:\Original_Text_File.txt"))
    {
        // write all content of the property to the file.
        writer.Write(TextProperty);    
    }
}

如果您想逐行分隔或对每一行进行特定处理,您可以使用StringBuilderclass.

private void button1_Click(object sender, EventArgs e)
{
    using (StreamReader reader = new StreamReader(@"C:\Original_Text_File.txt"))
    {
        StringBuilder builder = new StringBuilder();
        while (reader.Peek() >= 0) 
        {
            builder.AppendLine(reader.ReadLine());
        }
    }
}

并获取StringBuilder刚刚调用该ToString()方法的所有全部内容并将其写入文件:

private void button2_Click(object sender, EventArgs e)
{
    using (StreamWriter writer = new StreamWriter(@"C:\Original_Text_File.txt"))
    {
        writer.Write(builder.ToString());
    }
}
于 2012-12-28T22:32:15.103 回答
0

创建 a List<String>,将每一行读入其中。然后 String.Join 使用 a,作为分隔符加入列表。当然Clear在每一行的开头调用列表中的方法。

于 2012-12-28T22:30:14.607 回答
0
private void button1_Click(object sender, EventArgs e)
{
    using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
    {
        var i = 0;
        while (!Reader.EndOfStream)
        {
            if (i % 12 == 11)
            {
                //every 12 lines you read append new line instead of ,
                TextBox1.AppendText(string.Format("{0}\n", Reader.ReadLine()));
            }
            else
            {
                TextBox1.AppendText(string.Format("{0},", Reader.ReadLine()));
            }
            i++;
        }
    }
}

private void button2_Click(object sender, EventArgs e)
{
    using (StreamWriter Writer = new StreamWriter(@"C:\Original_Text_File.txt"))
    {
        using (StringReader Reader = new StringReader(TextBox1.Text))
        {
            while (true)
            {                
                var line = Reader.Readline()
                if(line !=null)
                {
                    Writer.WriteLine(Reader.Readline());
                }
                else
                {
                    break;
                }
            }
        }
    }
}
于 2012-12-28T22:57:25.230 回答
0

注意 - 如果有标记(空白行),我将把它作为一个示例来说明如何执行此操作,我在编写示例数据时错过了阅读示例数据。


像这样的东西应该可以工作,我没有测试,所以它可能有错别字:

注意:我寻找一个空行(而不是计数)以知道我在记录的末尾。这样,如果您添加另一个字段,代码将不会中断。

    using (StreamReader Reader = new StreamReader(@"C:\Original_Text_File.txt"))
    {
        StringBuilder aLine;
        boolean first = true;

        while (!Reader.EndOfStream)
        {
           // read source line
           string inLine = Reader.ReadLine();

           // if length is zero append to test box (we have a blank line between records)
           if (inLine.Length == 0)
           {
              TextBox1.AppendText(aLine.ToString());
              first = true;
           } 

           // add a comma if we are not the first
           if (!first)
           {
             aLine.Append(",");
           }
           aLine.Append(inLine);

           // next time we won't be first
           first = false;

        }
        TextBox1.AppendText(aLine.ToString());

    }           
于 2012-12-28T22:36:45.447 回答