none
One column textfile to mulitple column RRS feed

  • Question

  • Hi!

    I have a little trouble figuring this out i have a text file that looks like this

    June 15
    Thing
    345
    June 16
    Thing
    433
    June 16
    Del
    344

    I want to transform and save the text file so it looks like this whit:

    June 15,Thing,345
    June 16,Thing, 433
    June 16,Del,344

    Whit this code i manage to remove all the blank lines from the file but strugling with the rest: 

    private void test_Click(object sender, EventArgs e)
    {
    
    var lines = File.ReadAllLines("C:\\test.txt").Where(arg => !String.IsNullOrWhiteSpace(arg));
    File.WriteAllLines("C:\\test.txt", lines);
    
    }

    Best regards
    Petter

    Saturday, September 7, 2019 1:20 PM

Answers

  •     

    Hi,

    I made a sample:


    private void Button1_Click(object sender, EventArgs e)
    {
        // --- store text into array --------
        string textFile = @"C:\test\input.txt";
        System.Text.Encoding enc = System.Text.Encoding.GetEncoding("shift_jis");
        // -- read all lines into array
        string[] aryLines = System.IO.File.ReadAllLines(textFile, enc);
        // -- read all lines into textBox1
        textBox1.Text = System.IO.File.ReadAllText(textFile, enc);
        // --- store array element into textBox: 3 elements as 1 line ------
        textBox2.Text = "";
        for (int i = 1; i <= aryLines.Length; ++i)
        {
            int mod = i % 3;
            if (mod == 0)
            {
                textBox2.Text = textBox2.Text 
                            + aryLines[i - 3] + ", "
                            + aryLines[i - 2] + ", "
                            + aryLines[i - 1] + Environment.NewLine;
            }
        }
        // --- save file
        string filePath = @"C:\test\output.txt";
        StreamWriter sw = new StreamWriter(filePath, false, Encoding.ASCII);
        sw.Write(textBox2.Text);
        sw.Close();
    }
    I hope this will be helpful.

    Regards,   

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html


    Sunday, September 8, 2019 1:49 AM
  • Check this too:

    string [ ] lines =
        File
            .ReadLines( @"C:\test.txt" )
            .Select( s => s.Trim( ) )
            .Where( s => !string.IsNullOrWhiteSpace( s ) )
            .ToArray( );
     
    using( var sw = new StreamWriter( @"C:\test.txt" ) )
    {
        for( int i = 0; i + 3 <= lines.Length; i += 3 )
        {
            sw.WriteLine( "{0}, {1}, {2}", lines[i], lines[i + 1], lines[i + 2] );
        }
    }
    

     

    Last incomplete group, if any, is ignored.

    Sunday, September 8, 2019 6:05 AM

All replies

  •     

    Hi,

    I made a sample:


    private void Button1_Click(object sender, EventArgs e)
    {
        // --- store text into array --------
        string textFile = @"C:\test\input.txt";
        System.Text.Encoding enc = System.Text.Encoding.GetEncoding("shift_jis");
        // -- read all lines into array
        string[] aryLines = System.IO.File.ReadAllLines(textFile, enc);
        // -- read all lines into textBox1
        textBox1.Text = System.IO.File.ReadAllText(textFile, enc);
        // --- store array element into textBox: 3 elements as 1 line ------
        textBox2.Text = "";
        for (int i = 1; i <= aryLines.Length; ++i)
        {
            int mod = i % 3;
            if (mod == 0)
            {
                textBox2.Text = textBox2.Text 
                            + aryLines[i - 3] + ", "
                            + aryLines[i - 2] + ", "
                            + aryLines[i - 1] + Environment.NewLine;
            }
        }
        // --- save file
        string filePath = @"C:\test\output.txt";
        StreamWriter sw = new StreamWriter(filePath, false, Encoding.ASCII);
        sw.Write(textBox2.Text);
        sw.Close();
    }
    I hope this will be helpful.

    Regards,   

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html


    Sunday, September 8, 2019 1:49 AM
  • Check this too:

    string [ ] lines =
        File
            .ReadLines( @"C:\test.txt" )
            .Select( s => s.Trim( ) )
            .Where( s => !string.IsNullOrWhiteSpace( s ) )
            .ToArray( );
     
    using( var sw = new StreamWriter( @"C:\test.txt" ) )
    {
        for( int i = 0; i + 3 <= lines.Length; i += 3 )
        {
            sw.WriteLine( "{0}, {1}, {2}", lines[i], lines[i + 1], lines[i + 2] );
        }
    }
    

     

    Last incomplete group, if any, is ignored.

    Sunday, September 8, 2019 6:05 AM