") then delete the CRLF, and repeat the operation until you have only correct rows.

问题描述 投票:0回答:1
This is my code :

The problem is that i'm reading the file in one pass, so the line break on line 4 is removed and line 4 and line 5 are together...

column1|column2|column3|column4|column5 (\r\n)
column1|column2|column3|column4|column5 (\r\n)
column1|column2 (\r\n)
column2 (\r\n)
column2|column3|column4|column5 (\r\n)

column1|column2|column3|column4|column5 (\r\n)
column1|column2|column3|column4|column5 (\r\n)
column1|column2/column2/column2|column3|column4|column5 (\r\n)

I have this type of data in a text file (csv) : column1

String path = "test.csv";

// Read file
string[] readText = File.ReadAllLines(path);

// Empty the file
File.WriteAllText(path, String.Empty);

int x = 0;
int countheaders = 0;
int countlines;
using (StreamWriter writer = new StreamWriter(path))
{
    foreach (string s in readText)
    {
        if (x == 0)
        {
            countheaders = s.Where(c => c == '|').Count();
            x = 1;
        }

        countlines = 0;
        countlines = s.Where(d => d == '|').Count();
        if (countlines == countheaders)
        {
            writer.WriteLine(s);
        }
        else
        {
            string s2 = s;
            s2 = s2.ToString().TrimEnd('\r', '\n');
            writer.Write(s2);
        }
    }
}

我有这种类型的数据在一个文本文件(csv)。
c# string newline filestream carriage-return
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.